在主題 functions.php 註冊短代碼,編輯文章時輸入 [bdad] 或 [bdad size="300"] 即可在任意位置顯示廣告。
// functions.php
function bdad_shortcode($atts) {
$atts = shortcode_atts(['size' => '580'], $atts);
$isSingle = is_single();
if (!$isSingle) return '';
if ($atts['size'] === '300') {
return '
';
} else {
return '
';
}
}
add_shortcode('bdad', 'bdad_shortcode');
function dw(html) { document.write(html); }
function loadBaiduAd(cproId, type) {
// type: 'c' = 異步載入 c.js,'f' = 同步載入 f.js
var scriptUrl = type === 'c'
? 'https://cpro.baidustatic.com/cpro/ui/c.js'
: 'https://cpro.baidustatic.com/cpro/ui/f.js';
dw('');
dw('');
}
// 頁面任意位置呼叫
loadBaiduAd('u1674017', 'c'); // 300x250
loadBaiduAd('u1657518', 'c'); // 580x90
function loadBaiduAdAsync(cproId, containerId) {
var container = document.getElementById(containerId);
if (!container) return;
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://cpro.baidustatic.com/cpro/ui/c.js';
script.async = true;
// 必須先定義 cpro_id 全域變數
window.cpro_id = cproId;
container.appendChild(script);
}
// HTML 預留容器
//
//
https://cpro.baidustatic.com 避免混合內容警告。cpro_id 需在百度聯盟後台建立對應廣告位獲取。createElement + appendChild 或 innerHTML。