WordPress不同分类/文章调用不同模板

在网上翻了半天这个问题的答案,虽然有,但基本全胳膊少腿的。自己尝试了几下,做个记录。

说明:里面的数字为分类ID号,在后台编辑分类的地址上可以看到这个id号,对应写就行了。最后一个else是在之前没特别定义的分类样式。

<?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’);

}

?>

这个应该同时可以扩展为按不同标签等来进行设置。

以上代码分别修改的是archive.php和single.php,全部替换成如上代码。