最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

categories - how to exclude specific category types (uncategorized) from blog page?

programmeradmin1浏览0评论

I made one blog post page and 2 second page I made for uncategorized category page using this file category-uncategorized.php and now I want to remove this all uncategorized category from blog page.

and here is a code

<?php if(have_posts()) :
    while (have_posts()) :
        the_post();
        get_template_part('content', get_post_format());
    endwhile;?>
    <?php echo bootstrap_pagination($query) ?>
<?php else :
    echo '<p>No Content Found</p>';
    endif;?>

I made one blog post page and 2 second page I made for uncategorized category page using this file category-uncategorized.php and now I want to remove this all uncategorized category from blog page.

and here is a code

<?php if(have_posts()) :
    while (have_posts()) :
        the_post();
        get_template_part('content', get_post_format());
    endwhile;?>
    <?php echo bootstrap_pagination($query) ?>
<?php else :
    echo '<p>No Content Found</p>';
    endif;?>
Share Improve this question edited Jun 26, 2019 at 11:25 Rup 4,4004 gold badges29 silver badges29 bronze badges asked Jun 26, 2019 at 9:48 Vishal KunwerVishal Kunwer 1 1
  • When you say you "made one blog post page" do you mean you made a Page in wp-admin and you are showing all the Posts within that Page? Or do you just mean you created a Post in the Uncategorized Category, and now that post is showing up on your front page a.k.a. the "Posts page"? In either case you could use pre_get_posts to change the query on whichever URL you are talking about, to exclude that category. – WebElaine Commented Jun 26, 2019 at 13:12
Add a comment  | 

1 Answer 1

Reset to default 0

Please try code given below:

function blog_exclude_category( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'cat', '-1' );
    }
}
add_action( 'pre_get_posts', 'blog_exclude_category' );

Add this in your functions.php

发布评论

评论列表(0)

  1. 暂无评论