今天给大家分享一个worpress不同分类文章调用不同模板的方法,非常简单只需要两步即可完成。
使用不同的分类目录里的文章调用不同的模板,首先在function.php里,添加如下代码:
<?php if ( in_category(array( 5 )) ) { get_template_part('single-anli' ); } elseif ( in_category( 7 )) { get_template_part('single-all' ); } else { get_template_part('single-all' ); } ?>
复制一份single.php,命名为:single-*.php文件名(你可以根据自己的需要,制作多个 single-*.php 文件,通过修改每个single-*.php 文件的html结构和添加对应的CSS,就可以实现不同的文章页面样式 )。
将 single.php 里面除了 get_header(); get_footer(); get_sidebar(); 之外的所有内容改成:
<?php
if ( in_category('16') || post_is_in_descendant_category(16) )//可自行修改 这里包含分类目录里的文章和分类目录里的子分类目录里的文章
{
include(TEMPLATEPATH .'/single-16.php');
}
elseif ( in_category('7') || post_is_in_descendant_category(7) )//如果只有两类single.php,可以不要这段,如果是多类,则添加多个elseif
{
include(TEMPLATEPATH . '/single-7.php');
}
else{
include(TEMPLATEPATH . '/single-other.php');
}//给其他分类的文章调用的。
?>
相关阅读: