Archived topic
How to disable posts from showing on category page for specific category only.
21 replies · Started by Aaron on April 11, 2023
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
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
No, it would be for specific category archive pages only. Thanks :)
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.
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
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
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....
Yes, but keep the - negative sign. Can you double-check if the ID you set there is correct? Reference: https://www.wpbeginner.com/beginners-guide/how-to-find-post-category-tag-comments-or-user-id-in-wordpress/#find-category-tag-id:~:text=above%20is%207.-,How%20to%20Find%20Your%20WordPress%20Category%20ID%20and%20Tag%20ID,-Categories%20and%20tags
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.
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.
Hi there,
i assume you want to keep only the content that is in the Page Header ?
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.
Just a quick bump here :)
I'm seeing the Breadcrumbs and the title from the link you provided?
Are you referring to something else?
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.