标签: single模板

  • WordPress为某个分类添加特定的内容页模板

    如果想给某个分类目录下的内容页用不的模板,可以使用以下的方式实现。

    将以下这段代码放到functions.php中

    add_filter( 'single_template', 'get_wordpress_cat_template' ) ;
    function get_wodepress_cat_template( $single_template ) {
        global $post;//wodepress.com
        if ( is_category( 'news' ) || in_category( 'news' ) ) {
            $single_template = dirname( __FILE__ ) . '/single-news.php';
        }
        return $single_template;
    }

    上面的”news”可以是分别目录的别名,也可以是分类目录的ID。

    将代码添加了保存后,在主题文件夹中新建single-news.php文件。此时别名为news的分类目录下的内容页调用的的就是single-news.php对应的模板了。

  • 为不同文章形式选择不同的WordPress文章模板

    在写文章的时候选择不同的文章形式,然后打开文章的时候会调用不同文章形式的模板。比如,文章形式为video ,就调用single-video.php模板,其它文章形式类似,可以添加多个文章样式。

    //为不同文章形式的内容添加不同的single页面
    add_action('template_include', 'load_single_template');
    function load_single_template($template) {
      $new_template ='';
      // single post template
      if( is_single() ) {
        global $post;
        if ( has_post_format( 'video' )) {// 文章形式为video
          $new_template = locate_template(array('single-video.php' ));// 就调用single-video.php模板
        }
        if ( has_post_format( 'image' )) {// 文章形式为image
          $new_template = locate_template(array('single-image.php' ));// 就调用ssingle-image.php模板
        }
    // 这里可以添加其他文章形式的模板
      }
      return (''!= $new_template) ? $new_template : $template;
    }

    将以上代码添加到functions.php文件中即可。

  • wordpress根据文章分类自动调用指定页面模板

    通过wordpress分类目录的别名来调用指定的single模板

     add_action('template_include', 'load_single_template');
     function load_single_template($template) {
     $new_template = '';
     if( is_single() ) {
     global $post;
     // 新闻
     if( has_term('news', 'category', $post) ) {
     $new_template = locate_template(array('single-newsinfo.php' ));
            }
     // 团队
     if( has_term('team', 'category', $post) ) {
     $new_template = locate_template(array('single-team.php' ));
            }
     // 案例
     if( has_term('case', 'category', $post) ) {
     $new_template = locate_template(array('single-case.php' ));
            }
     // 产品
     if( has_term('product', 'category', $post) ) {
     $new_template = locate_template(array('single-product.php' ));
            }
         }
     return ('' != $new_template) ? $new_template : $template;
     }

    把上面的代码,添加到functions.php文件中即可。