如果想让某个分类的文章页面样式有别于其它分类,我们可以使用自定义的模板的方法实现。例如,我们准备让名称为 WordPress 的分类文章使用有别于其它分类的模板样式,

首先在所用主题根目录新建一个名称 single-wordpress.php的模板文件。将以下代码片段添加到您的当前主题的 functions.php 文件:

 1 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   }
View Code

相关文章:

  • 2021-06-06
  • 2021-10-13
  • 2021-09-20
  • 2021-04-30
  • 2021-07-18
  • 2022-02-16
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-02-15
  • 2021-07-18
  • 2021-12-17
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案