WordPress实现不同分类调用不同模板
时间:2020-11-23
来源:网络
浏览:498
今天群里有朋友问WordPress怎样实现子分类和父分类调用不同模板?其实原理就是不同分类调用不同的模板。
我们知道in_category() 函数可以通过分类别名或ID判断当前文章所属的分类,而且可以直接在循环(Loop)内部和外部使用。首先我们在category.php文件,复制下面的代码。
<?php
if ( in_category(array( 2,3 )) ) {//多个栏目id
get_template_part('category-product' );
} elseif ( in_category( 7 )) {//单个栏目id
get_template_part('category-case' );
} else {//其他调用默认模板
get_template_part('category-default' );
}
?>
然后分别创建三个category-product.php,category-case.php,category-default.php,根据需要加入不同的代码。
当然也支持分类别名slug调用:
in_category('themes')//单个别名
in_category( array( 'themes','plugins','develop') )//多个别名
同样的道理,我们可以根据不同的分类制作不同的内容页模板,把下面代码复制到single.php。
<?php
if ( in_category(array( 2,3 )) ) {//多个栏目id
get_template_part('single-product' );
} elseif ( in_category( 7 )) {//单个栏目id
get_template_part('single-case' );
} else {//其他调用默认模板
get_template_part('single-default' );
}
?>
然后分别创建三个single-product.php,single-case.php,single-default.php,根据需要加入不同的代码,即可。
标题:WordPress实现不同分类调用不同模板
链接:https://www.bigetheme.cn/4155.html
版权:文章转载自网络,如有侵权,请联系删除!