标签: 文章模板调用

  • 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文件中即可。