从Wordpress循环中删除未分类的类别

时间:2013-08-22 04:03:11

标签: php wordpress loops

我想按类别列出自定义帖子类型的所有帖子,所以我想出了这个代码(见下文)但是我看到这个未分类的类别我要删除。我尝试了查询cat=$cat_id&exclude="1"&post_type=locations,但它无效。有任何想法吗?谢谢

<div class="entry-content">
    <?php
    $cats = get_categories();

    foreach ($cats as $cat) {
        $cat_id= $cat->term_id;
        echo "<h2>".$cat->name."</h2>";
        query_posts("cat=$cat_id&exclude="1"&post_type=locations");

        if (have_posts()) : while (have_posts()) : the_post(); ?>
    <?php ?>
    <a href="<?php the_permalink();?>"><?php the_title(); ?></a>
    <?php echo '<hr/>'; ?>

    <?php endwhile; endif;  ?>
    <?php } ?>
    <?php wp_reset_postdata(); ?>
</div><!-- .entry-content -->

4 个答案:

答案 0 :(得分:1)

这已经很老了,但我偶然发现了它,所以如果它与任何人相关,我认为这就是答案:

由于您是按ID指定每个类别,因此当您的术语ID为1时,您试图排除您指定的类别。因此,在循环的第一次迭代中,您说的是

cat=1&exclude=1&post_type=locations

由于您准确指定了排除的内容,因此我认为“包含”查询值超过了“独占”查询值。如果是这种情况,你可以这样做(甚至保存查询!):

$cats = get_categories();

foreach ( $cats as $cat ) {
    $cat_id = $cat->term_id;

    if ( $cat_id == 1 )
        continue; // skip 'uncategorized'

    echo "<h2>".$cat->name."</h2>";

    query_posts("cat=$cat_id&post_type=locations");

    ?>{the rest}<?php
}

请注意,我不建议使用query_posts,而是应使用get_posts()new WP_Query。进一步阅读: https://developer.wordpress.com/2012/05/14/querying-posts-without-query_posts/

答案 1 :(得分:0)

`query_posts( 'cat=-3' );`

// 3是您要排除的类别ID  看更多参考。关于query_posts QUERY POSTS

答案 2 :(得分:-1)

问题是你的报价 试试这个:

 query_posts("cat=$cat_id&exclude=1&post_type=locations");

希望这会对你有所帮助。

答案 3 :(得分:-2)

尝试这个;

转到外观然后点击Widget, 打开小部件后,单击Primary Widget区域, 你会看到“类别” 只需单击,按住鼠标左键并移至 “非活动小部件”组。 当你回到你的网站时,你将不会再看到未分类。 : - ))

相关问题