都说wordpress很强大,但似乎也不是万能的吧。也观察了不少的wordpress博客,外观上做的有特色的,不是特别多,至少就CMS主题来说。要随意做分类不同的子还真麻烦.上一篇文章WordPress模版调用的PHP代码总有些不足.下面补充一下.
一、不同分类调用不同模板
<?php
$post = $wp_query->post;
if ( in_category(‘7′) ) {
include(TEMPLATEPATH . ‘/archive-view.php’);
}
else if ( in_category(‘12′) ) {
include(TEMPLATEPATH . ‘/single12.php’);
}
else if ( in_category(‘42′) ) {
include(TEMPLATEPATH . ‘/single42.php’);
}
else {
include(TEMPLATEPATH . ‘/archive-other.php’);
}
?>
二、为不同文章按照分类来调用不同模板
<?php
$post = $wp_query->post;
if ( in_category(‘7′) ) {
include(TEMPLATEPATH . ‘/single-view.php’);
}
else if ( in_category(‘3′)) {
include(TEMPLATEPATH . ‘/single-case.php’);
}
else if ( in_category(‘42′) ) {
include(TEMPLATEPATH . ‘/single42.php’);
}
else {
include(TEMPLATEPATH . ‘/archive-other.php’);
}
?>
三、几点解释
1、这个应该同时可以扩展为按不同标签等来进行设置。
2、里面的数字为分类ID号,在后台编辑分类的地址上可以看到这个id号,对应写就行了。最后一个else是在之前没特别定义的分类样式。
3、以上代码分别修改的是archive.php和single.php,全部替换成如上代码。
本文作者墨绿来自广州SEO博客

14 Comments, Comment or Ping
阅微博客
不同页面调用不同模板还是比较实用的。
二 26th, 2011
冲棒
厉害,领教了,谢谢分享
三 2nd, 2011
情商是鱼,智商是水
这个功能挺创新,试试看。
三 4th, 2011
微微健康网
哦哦.有机会也试试看!
三 9th, 2011
长春网站优化
很好,我也试试
三 15th, 2011
岁月无痕
WP就是强大。谢谢分享。
三 17th, 2011
岁月无痕
好东西,有收获。
三 18th, 2011
201不锈钢管
学习呢 还可以这样的第一次知道
三 19th, 2011
妇宝宁
尝试下!
三 22nd, 2011
西安家装网
调用的太强大了哈!
三 22nd, 2011
www.gznpi.net
感谢分享,收藏了
三 23rd, 2011
www.yq001.com
实用!
四 28th, 2011
文秘之家
第一次来 踩踩
七 5th, 2011
济南456解放军医院
支持wordpress,很给力
七 10th, 2011
Reply to “为不同文章按照分类来调用不同模板”