日志标签 ‘百度联盟’

Warning: Use of undefined constant archives - assumed 'archives' (this will throw an Error in a future version of PHP) in /www/wwwroot/daibei.info/wp-content/themes/cordobo-green-park-2/archive.php on line 32

Warning: Use of undefined constant page - assumed 'page' (this will throw an Error in a future version of PHP) in /www/wwwroot/daibei.info/wp-content/themes/cordobo-green-park-2/archive.php on line 32

Warning: A non-numeric value encountered in /www/wwwroot/daibei.info/wp-content/themes/cordobo-green-park-2/archive.php on line 32

Warning: A non-numeric value encountered in /www/wwwroot/daibei.info/wp-content/themes/cordobo-green-park-2/archive.php on line 32
class="post-1402 post type-post status-publish format-standard hentry category-grass-roots-webmaster tag-alimama tag-google-adsense tag-430 tag-55 tag-164 tag-906">

WordPress广告随机显示的方法

2011年3月11日

因为每天都看到同样的广告,感到很厌烦,同时,由于申请了Google Adsense、百度联盟、凡客诚品联盟、阿里妈妈等一系列的广告联盟,可是博客里面只有一两个广告位,那怎么办呢?

据说是有wordpress的广告插件,可以实现广告随机显示,但是如果插件太多的话,很容易导致WP速度变慢,于是博主们一般都希望能不用插件即可实现广告的随机显示。

Google了一下,发现不用插件实现广告随机显示的方法有好多,自己测试了一下,发现有两种方法是比较不错的。

方法一:来自求索阁:wordpress中不同广告联盟随机显示

<?php if ( rand(0, 1) == 0) { ?>
广告代码1
<?php } else { ?>
广告代码2
<?php } ?>

很简单,将“广告代码1”、“广告代码2”替换为您的Google Adsense或者百度联盟广告代码就OK了。

不过,有个缺点就是貌似只能两个广告代码交替出现,如果有两个以上的广告想同时出现的话,貌似就不是很容易实现了。

方法二:来自壹品集让你的WordPress 显示随机广告

<?php
    $ad = array(
        ‘广告代码_1’,
        ‘广告代码_2’,
        ‘广告代码_3’,
    );
    $rand = array_rand($ad);
    echo $ad[$rand];
?>

相对于第一种方法,这个可以实现多个广告代码交替出现。

不过,在实际使用中却出现了问题:如果使用了百度联盟的广告代码的话,则WordPress会报错。不知道为什么会出现这种情况。由于不懂代码,于是最佳的解决办法是,在这个代码中,不加百度联盟广告……