[Support request] How to disable posts from showing on category page for specific category only.

Home Forums Support [Support request] How to disable posts from showing on category page for specific category only.

Home Forums Support How to disable posts from showing on category page for specific category only.

  • This topic has 21 replies, 3 voices, and was last updated 12 months ago by Fernando.
Viewing 15 posts - 1 through 15 (of 22 total)
  • Author
    Posts
  • #2606555
    Aaron

    I want to make it so just a couple of my category pages do not display posts in that category.

    I plan to use block hooks to insert custom content on these pages and will display the posts manually.
    The rest of my category pages should behave normally.

    Is there a way to achieve this? Thanks

    #2606591
    Fernando
    Customer Support

    Hi Aaron,

    Would this be for all Category Archive pages? If so, try adding this snippet:

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

    Adding PHP: https://docs.generatepress.com/article/adding-php/#code-snippets

    #2609014
    Aaron

    No, it would be for specific category archive pages only. Thanks πŸ™‚

    #2609024
    Fernando
    Customer Support

    Try this:

    function exclude_category( $query ) {
    if ( $query->is_category('YOUR-CATEGORY-SLUG-HERE') && $query->is_main_query() ) {
    $query->set( 'cat', '-1' );
    }
    }
    add_action( 'pre_get_posts', 'exclude_category' );

    Replace YOUR-CATEGORY-SLUG-HERE with your category’s slug in the code.

    #2609146
    Aaron

    Hi, I tried and the posts are still showing. I used the attached URL as a test category.
    I replaced the YOUR-CATEGORY-SLUG-HERE with the word “garage”. And the posts are still showing as you can see.

    I added the code by adding to the child themes functions.php file

    #2609160
    Fernando
    Customer Support

    Oh, I forgot to mention that you need to replace 1 with the ID of the Category you wish to exclude.

    I also updated the code here: https://generatepress.com/forums/topic/how-to-disable-posts-from-showing-on-category-page-for-specific-category-only/#post-2609024

    #2609170
    Aaron

    I see a -1 in the code – is that what I need to replace with the category ID?
    Ive tried it a few different ways and it still doesn’t do anything….

    #2609219
    Fernando
    Customer Support
    #2610296
    Aaron

    Ok that works, but it also stops my breadcrumbs element working which is set to display at generate_before_archive_title
    Also the archive title is not showing anymore.
    It seems elements are not showing at all on that category when I use that code.

    My plan is to insert custom content using a hook onto the category archive page, I want it all as standard except no posts to show automatically.

    #2610307
    Aaron

    Ive added a link in the hidden details – this is one of the categories I need to remove the posts from. You will see why.

    #2610476
    David
    Staff
    Customer Support

    Hi there,

    i assume you want to keep only the content that is in the Page Header ?

    #2611036
    Aaron

    Hi David,
    I want to keep the page title (if possible, otherwise Ill just add it back in) and my elements. The ones that display content are
    generate_after_archive_title – content you can see in the URL I gave
    generate_before_archive_title – breadcrumbs

    The only thing I actual want to remove is the post lists for the category.

    #2616447
    Aaron

    Just a quick bump here πŸ™‚

    #2616474
    Fernando
    Customer Support

    I’m seeing the Breadcrumbs and the title from the link you provided?

    Are you referring to something else?

    #2616487
    Aaron

    That’s because I haven’t got the code you gave me in place because it removes all content as I explained earlier. Th example I gave is so that you can see why I want to remove the post list at the bottom which is still there.

    possible, otherwise Ill just add it back in) and my elements. The ones that display content are
    generate_after_archive_title – content you can see in the URL I gave
    generate_before_archive_title – breadcrumbs

    The only thing I actual want to remove is the post lists for the category.

Viewing 15 posts - 1 through 15 (of 22 total)
  • You must be logged in to reply to this topic.