5/5 - (3 bình chọn)

Hôm nay mình xin chia sẻ đoạn code hiển thị tỷ giá ngoại tệ của Vietcombank

Code hiển thị tỷ giá ngoại tệ VCB:

function tygiangoaite_vcb() {
    ob_start();
    ?>
        <table class="table table-hover">
            <thead>
                <tr>
                    <th colspan="5" style="text-align:center"><h2>TỶ GIÁ NGOẠI TỆ</h2></th>
                </tr>
                <tr>
                    <th>MÃ NGOẠI TỆ</th>
                    <th>TÊN NGOẠI TỆ</th>
                    <th>MUA TIỀN MẶT</th>
                    <th>MUA CHUYỂN KHOẢN</th>
                    <th>BÁN</th>
                </tr>
            </thead>
            <tbody>
                <?php
            function check_broken_link($url){
                $handle   = curl_init($url);
                if (false === $handle)
                {
                    return false;
                }
                curl_setopt($handle, CURLOPT_HEADER, false);
                curl_setopt($handle, CURLOPT_FAILONERROR, true);  // this works
                curl_setopt($handle, CURLOPT_NOBODY, true);
                curl_setopt($handle, CURLOPT_RETURNTRANSFER, false);
                $connectable = curl_exec($handle);
                curl_close($handle);    
                return $connectable;
            }
            $url = "http://www.vietcombank.com.vn/exchangerates/ExrateXML.aspx";
            $thoi_gian_cap_nhat = "";
            if(check_broken_link($url) === TRUE){
                $xml = file_get_contents($url);
                $data = simplexml_load_string($xml);
                if ($data === false) {
                    echo '<tr class="danger"><th colspan="5" style="text-align:center"><h5>DỮ LIỆU BỊ LỖI</h5></th></tr>';
                }
                else {										
                    $thoi_gian_cap_nhat = $data->DateTime;
                    $ty_gia = $data->Exrate;
                    $i=1;
                    foreach($ty_gia as $ngoai_te) {
                        $ma = $ngoai_te['CurrencyCode'];
                        $ten = $ngoai_te['CurrencyName'];
                        $gia_mua = $ngoai_te['Buy'];
                        $gia_chuyen_khoan = $ngoai_te['Transfer'];
                        $gia_ban = $ngoai_te['Sell'];
                        $class_color = "success";
                        if($i%2 == 0){ $class_color = "info";}
                        echo "<tr class='".$class_color."'>";
                        echo "<td>".$ma."</td>";
                        echo "<td>".$ten."</td>";
                        echo "<td>".$gia_mua."</td>";
                        echo "<td>".$gia_chuyen_khoan."</td>";
                        echo "<td>".$gia_ban."</td>";
                        echo "</tr>";
                        $i++;
                    }
                }
            }
            else{
                echo '<tr class="danger"><th colspan="5" style="text-align:center"><h5>KHÔNG THỂ KẾT NỐI ĐẾN MÁY CHỦ VIETCOMBANK</h5></th></tr>';
            }
                ?>
            </tbody>

            <tfoot>
                <?php if($thoi_gian_cap_nhat != ""){
                    $thoi_gian_cap_nhat = date_format(date_create($thoi_gian_cap_nhat),'H:i:s d-m-Y');
                    $thoi_gian_cap_nhat = explode(' ', $thoi_gian_cap_nhat);
                    $gio = $thoi_gian_cap_nhat[0];
                    $ngay = $thoi_gian_cap_nhat[1];
                ?>
                <tr>
                    <th colspan="5">Tỷ giá được cập nhật lúc <?php echo $gio; ?> ngày <?php echo $ngay; ?> và chỉ mang tính chất tham khảo.</th>
                </tr>
                <?php } ?>
            </tfoot>

        </table>
<?php
        $tygia_vcb = ob_get_contents();
        ob_end_clean();

        return $tygia_vcb;	
}
add_shortcode('tygiangoaite_vcb','tygiangoaite_vcb');

Cách sử dụng:

  1. Bạn truy cập vào Giao diện -> Sửa giao diện -> chọn functions.php của theme đang dùng. Dán code vào
  2. Bước tiếp theo là chèn shortcode [tygiangoaite_vcb]  vào nơi bạn muốn hiển thị

Note: Nếu chèn vèo widget mà không hiển thị, bạn chèn thêm code sau vào functions.php của theme đang dùng.

add_filter( 'widget_text', 'do_shortcode' );

Cảm ơn đã ghé thăm!

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *