I would like to hide posts belonging to a certain category on the front page

Homepage Forums Community General WordPress Help I would like to hide posts belonging to a certain category on the front page

I would like to hide posts belonging to a certain category on the front page

This topic contains 5 replies, has 2 voices, and was last updated by  iceable 3 years, 3 months ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #9907
    I would like to hide posts belonging to a certain category on the front page

    onone2
    Participant

    I’m trying to exclude posts that belong to a certain category from posting on the front page and only have them post to their category. The category I am trying to do this with is titled: Blog. In order to do this I have tried to add code to the loop in the index.php file.

    I have tried to add it so it looks like this:

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <?php if ( !(in_category(‘Blog’)) || !is_home() ) { ?>
    <!– Output the post here –>
    <?php } ?>
    <?php endwhile; ?>

    and I have also tried this:


    <?php if ( is_home() ) {
    $query = new WP_Query( ‘cat=-Blog’ );
    if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
    } else {

    ?>

    Neither of these have worked.

    Can you provide a code that will work?

    Or do you have any suggestions on how to achieve this in a different way?

    #9908
    Reply to: I would like to hide posts belonging to a certain category on the front page

    iceable
    Keymaster
    Support
    Happy with my help?
    Buy me a beer!

    Your code examples are pretty much on the right track, but the best and most efficient way to do it is to edit functions.php instead, and add this at the end (and keep the original index.php) :

    function exclude_blog_category( $query ) {
        if ( $query->is_home() && $query->is_main_query() ):
            $query->set( 'cat', '-3' );
        endif;
    }
    add_action( 'pre_get_posts', 'exclude_blog_category' );

    You need to use the ID of the category with a minus sign (-3 in this example) instead of the slug.
    To find your category’s ID, you can go to Posts > Categories, hover your mouse over the “edit” button of your blog category and look at the URL it points to, it will look like …taxonomy=category&tag_ID=3…

    If you are interested to learn more, this page http://codex.wordpress.org/Function_Reference/query_posts of the codex explains why creating a new query is inefficient and here: http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts you’ll get details about the pre_get_posts hooks which allows to efficiently alter the main query.

    I hope this helps! 🙂

    [moving this thread to General WP since this is not specific to a theme]

    #9910
    Reply to: I would like to hide posts belonging to a certain category on the front page

    onone2
    Participant

    I tried adding the code to the functions.php and it left me with a blank page. The category is 5 so I wrote your code as:

    function exclude_blog_category( $query ) {
    if ( $query->is_home() && $query->is_main_query() ):
    $query->set( ‘cat’, ‘-5’ );
    endif;
    }
    add_action( ‘pre_get_posts’, ‘exclude_blog_category’ );

    And added it to the bottom of the file.

    Am I missing something here?

    #9911
    Reply to: I would like to hide posts belonging to a certain category on the front page

    iceable
    Keymaster
    Support
    Happy with my help?
    Buy me a beer!

    Two things I can think of:

    – Make sure to add this at the end of functions.php, but BEFORE the last ?> tag!
    – Pay close attention to the quotes symbols ( ‘ ), they can sometimes be corrupt when copy/pasting from the forum. Try to delete one and re-type it from your keyboard, and if it looks different do it with all of them!

    If you still have troubles I can offer to have a look and help you directly on your site if you wish to provide me with a temporary login/pass (you can use the “set as private reply” box when replying to not post it publicly).

    #9913
    Reply to: I would like to hide posts belonging to a certain category on the front page

    onone2
    Participant

    Um… I was adding after the closing tag – doh! I’m blaming “late-night-staring-at-too-much-code” fatigue. Ha!

    Thank you so much, works perfectly. Great response and support.

    #9920
    Reply to: I would like to hide posts belonging to a certain category on the front page

    iceable
    Keymaster
    Support
    Happy with my help?
    Buy me a beer!

    No worry, staring-at-too-much-code fatigue happens all the time 😉 Glad you got it sorted!

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘I would like to hide posts belonging to a certain category on the front page’ is closed to new replies.


PayPal Verified
Secured by PayPal
Secure Credit Card payments powered by Stripe
SiteLock