// 6 个广告分组按权重随机显示 function weighted_ad_group_display() { // 6 个分组的 ID 和对应权重 $groups = [ 240 => 10, // Group 1 冠名 - 最高 241 => 7, // Group 2 至尊 242 => 5, // Group 3 奖项 243 => 4, // Group 4 铂金 244 => 3, // Group 5 黄金 245 => 2, // Group 6 Others - 最低 ]; // 加权随机算法 $total = array_sum($groups); $rand = mt_rand(1, $total); $cumulative = 0; foreach ($groups as $group_id => $weight) { $cumulative += $weight; if ($rand <= $cumulative) { // 用 Advanced Ads 的函数显示选中的分组 if (function_exists('the_ad_group')) { ob_start(); the_ad_group($group_id); return ob_get_clean(); } } } return ''; } add_shortcode('weighted_ad', 'weighted_ad_group_display');
Canadian Chinese Construction Association