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 1 year, 4 months ago by Fernando.
-
AuthorPosts
-
April 11, 2023 at 10:40 pm #2606555Aaron
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
April 11, 2023 at 11:29 pm #2606591Fernando Customer SupportHi 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
April 13, 2023 at 4:52 pm #2609014AaronNo, it would be for specific category archive pages only. Thanks π
April 13, 2023 at 5:12 pm #2609024Fernando Customer SupportTry 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.April 13, 2023 at 8:23 pm #2609146AaronHi, 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
April 13, 2023 at 8:43 pm #2609160Fernando Customer SupportOh, 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
April 13, 2023 at 8:58 pm #2609170AaronI 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….April 13, 2023 at 9:56 pm #2609219Fernando Customer SupportYes, 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%20tagsApril 14, 2023 at 10:07 pm #2610296AaronOk 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.
April 14, 2023 at 11:12 pm #2610307AaronIve added a link in the hidden details – this is one of the categories I need to remove the posts from. You will see why.
April 15, 2023 at 4:14 am #2610476DavidStaffCustomer SupportHi there,
i assume you want to keep only the content that is in the Page Header ?
April 15, 2023 at 10:33 pm #2611036AaronHi 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 – breadcrumbsThe only thing I actual want to remove is the post lists for the category.
April 20, 2023 at 9:00 pm #2616447AaronJust a quick bump here π
April 20, 2023 at 10:22 pm #2616474Fernando Customer SupportI’m seeing the Breadcrumbs and the title from the link you provided?
Are you referring to something else?
April 20, 2023 at 10:51 pm #2616487AaronThat’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 β breadcrumbsThe only thing I actual want to remove is the post lists for the category.
-
AuthorPosts
- You must be logged in to reply to this topic.