关于wp模版制作,根据分类ID显示不同的广告
本帖最后由 termjoy 于 2011-11-29 10:26 编辑根据分类ID不同显示不同的广告。或者随机显示广告。。需要懂点代码的人再买,不懂代码的就不要买了。
在functions.php文件中加入:
function get_category_root_id($cat)
{
$this_category = get_category($cat);
while($this_category->category_parent)
{
$this_category = get_category($this_category->category_parent);
}
return $this_category->term_id;
}
function get_article_category_ID()
{
$category = get_the_category();
return $category->cat_ID;
}
在 Single.php 页面基本调用方法如下:<?php echo get_category_root_id(get_article_category_ID()); ?>好,接下来对这段代码修改。。在Single.php文件中,你需要防广告的地方添加如下代码。。。
。。。<?php $cat_id = get_category_root_id(get_article_category_ID()); ?>
<?php if ($cat_id==1 OR $cat_id==3) :?>
<div style="float:left">
<?php
$ad = array(
'广告代码_1',
'广告代码_2',
'广告代码_3',
); //广告代码随机数组
$rand = array_rand($ad); //获取随机数组的键
echo $ad[$rand]; //输出随机数组的值,也就是随机广告
?>
</div>
<?php endif; ?>解释一下,当分类id=1或者等于3时,随机显示数组中的 广告代码_1,广告代码_2,广告代码_3
当然,你可以不用随机显示,将div之间的内容替换掉就好了。代码如下:<div style="float:left">你的广告代码</div> 插件还是需要自己修改模板? 收藏来,有时间了来看下 回复 2# 嗯嗯
自己修改模版。。。不会搞插件,也没那么多时间研究那个。。
页:
[1]