WordPress判断不同分类调用不同的模板

时间:2014-01-26所属栏目: WordPress教程 来源: 热度:

要想用wordpress搭建一个CMS,或者让“列表”页更加丰富互有区别,这就需要让分类 category.php 页面有所差别。出于这个目的,向安同学请教了一番获得答案,今记录下来。

其原理就是在原有语句的基础上加上判断:

<?php
/* Start the Loop */
while ( have_posts() ) : the_post();

	get_template_part( 'content', get_post_format() );

endwhile;

twentytwelve_content_nav( 'nav-below' );
?>

如此判断:

<?php
/* Start the Loop */
while ( have_posts() ) : the_post();

	if (is_category('1'))
		get_template_part( 'content-style1', get_post_format() );
	elseif (is_category('2'))
		get_template_part( 'content-style2', get_post_format() );
	else 
		get_template_part( 'content', get_post_format() );

endwhile;

twentytwelve_content_nav( 'nav-below' );
?>

这个输出结果为:当分类目录ID为1的时候,category.php调用content-style1.php内容页面;当分类目录ID为2的时候,category.php调用content-style2.php内容页面,否则使用content.php,即默认模板。

如果某两个分类调用同一个内容模板页,那可以如此简写:

if (is_category('4') || is_category('5'))
	get_template_part( 'content-style3', get_post_format() );

有个这个秘籍就可以玩出更多花样了。

标签:
评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)