【问题标题】:Custom category template is displaying all categories自定义类别模板显示所有类别
【发布时间】:2013-02-15 02:35:17
【问题描述】:

这是模板。我的类别 slug 是“新闻”,类别 ID 是 40。我将 php 文件命名为 category-news.php 和 category-40.php。这两个文件都提供了正确的模板,但是当我想要的只是单个类别时,它会显示所有类别。为了显示单个类别,我必须更改什么?

<?php
/*
Template Name: Category PAGE
*/ 
get_header();?>

<?php
    if ( have_posts() ) : while ( have_posts() ) : the_post();

    $blog_style = $hana_post_meta['blog_style'];
    if($blog_style=='fluid') $hana_page_options['layout'] = 'full';
?>
<div class="blog-container <?php echo $blog_style;?>-blog">

    <div id="content" class="<?php echo ($blog_style!='fluid')?vintage_content_class():'';?>">

        <?php
        //query posts
        $postsPerPage=hana_get_opt('_post_per_page_on_blog')==''?5:hana_get_opt('_post_per_page_on_blog');
        $excludeCat=explode(',',hana_get_opt('_exclude_cat_from_blog'));
        query_posts(array(
            'category__not_in' => $excludeCat,
            'paged' => get_query_var('paged'),
            'posts_per_page' => hana_get_opt('_post_per_page_on_blog')
        ));

        $blog_loop = array();
        $blog_loop['layout'] = $hana_page_options['layout'];
        $blog_loop['column'] = $hana_post_meta['blogcolumn'];

        get_template_part( 'loop', $blog_style );

        wp_reset_query();?>

    </div>

    <?php get_sidebar();?>
    <?php vintage_get_addtional_products(); ?>
</div><?php

endwhile; endif;?>    

<?php get_footer(); ?>

【问题讨论】:

  • 我必须更改哪些内容才能使用此模板显示单个类别而不是全部?

标签: php wordpress templates categories


【解决方案1】:

query_posts() 需要在循环之前。 See the Wordpress Function Reference for query_posts().

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多