如果想讓某個分類的文章頁面樣式有別于其它分類,我們可以使用自定義的模板的方法實現。例如,我們準備讓名稱為 WordPress 的分類文章使用有別于其它分類的模板樣式,今天就為大家介紹下如何自定義WordPress文章頁面模板,希望可以幫助到大家。

首先在所用主題根目錄新建一個名稱 single-wordpress.php的模板文件。將以下代碼片段添加到您的當前主題的 functions.php 文件:
add_action(‘template_include’, ‘load_single_template’);
2 function load_single_template($template) {
3 $new_template = ”;
4 // single post template
5 if( is_single() ) {
6 global $post;
7 // ‘wordpress’ is category slugs
8 if( has_term(‘wordpress’, ‘category’, $post) ) {
9 // use template file single-wordpress.php
10 $new_template = locate_template(array(‘single-wordpress.php’ ));
11 }
12 }
13 return (” != $new_template) ? $new_template : $template;
14 }
上面的代碼將指定 WordPress 分類的文章,使用 single-wordpress.php 模板文件。同理,你可以重復以上的步驟,讓其它分類也可以使用自定義模板。





