今天網友問ytkah:wordpress不同分類如何調用不同的模板。我們知道in_category() 函數可以通過分類別名或ID判斷當前文章所屬的分類,而且可以直接在循環(Loop)內部和外部使用。首先創建一個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,根據需要加入不同的代碼
當然也支持別名slug調用
in_category('themes')//單個別名 in_category( array( 'themes','plugins','develop') )//多個別名
同樣的道理,我們可以根據不同的分類制作不同的分類頁模板,把sing改為category就可以
<?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' ); } ?>
注意is_category和in_category的區別
Try in_category() which checks if the current post is in a particular category.
The is_category() function checks if a category archive is being displayed.
有這方面需求的朋友可以試試