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:
functiontygiangoaite_vcb() {ob_start();?><tableclass="table table-hover"><thead><tr><thcolspan="5"style="text-align:center"><h2>TỶGIÁNGOẠITỆ</h2></th></tr><tr><th>MÃNGOẠITỆ</th><th>TÊNNGOẠITỆ</th><th>MUATIỀNMẶT</th><th>MUACHUYỂNKHOẢN</th><th>BÁN</th></tr></thead><tbody><?phpfunctioncheck_broken_link($url){ $handle =curl_init($url);if (false=== $handle) {returnfalse; }curl_setopt($handle,CURLOPT_HEADER,false);curl_setopt($handle,CURLOPT_FAILONERROR,true); // this workscurl_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><?phpif($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><thcolspan="5">Tỷgiáđượccậpnhậtlúc<?phpecho $gio; ?>ngày<?phpecho $ngay; ?>vàchỉmangtínhchấtthamkhả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:
- 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
- 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!