- This topic has 18 replies, 4 voices, and was last updated 5 years, 2 months ago by
Elvin.
-
AuthorPosts
-
March 22, 2021 at 8:01 am #1705244
Tobias
hey there,
I tried a few hours so far getting this done by myself, but I was not able to find a solution for my problem here.
I would love to hide specific category (id=1) from the home, archive and search pages. (see attached screenshot) The category should not be excluded, just hidden frontend, but its content should be displayed. hiding the parent category. In my screenshot this post has 2 categories and category b should be hidden and there should only category b displayed.
now:
https://snipboard.io/g4TdoU.jpgshould be like this:
https://snipboard.io/cq8g9C.jpgsorry for bad explanation
March 22, 2021 at 8:30 am #1705291David
StaffCustomer SupportHi there,
Tom provides a solution here for Post Tags:
The same principles should apply – but instead of:
term_links-post_tagfilteryou would use the:
term_links-post_categoryfilterMarch 22, 2021 at 8:48 am #1705323Tobias
hey David,
thanks for your fast reply, can you help me with the code? i am kind of too bad in coding to switch the code from tags to categories 🙁
best regards and thank you for your time!
March 22, 2021 at 9:06 am #1705345David
StaffCustomer SupportTry this:
add_filter( "term_links-post_cateogory", function( $term_links ) { $result = array(); $exclude_cateogories = array( 'some cateogory', 'another cateogory', 'third cateogory' ); foreach ( $term_links as $link ) { foreach ( $exclude_cateogories as $cateogory ) { if ( stripos( $link, $cateogory ) !== false ) continue 2; } $result[] = $link; } return $result; } );In the Array:
$exclude_cateogories = array( 'some cateogory', 'another cateogory', 'third cateogory' );you simply need to add the names of categories you wish to remove.
March 22, 2021 at 10:46 am #1705481Tobias
Hey Dave,
it is not working, even if I corrected cateogory and cateogories 🙁
can you check it again for me?Thanks and best regards
March 22, 2021 at 7:38 pm #1705862Elvin
StaffCustomer SupportHi there,
Can you try correcting the spelling of category? it seems like there was a misspell on the code.
Example:
add_filter( "term_links-post_category", function( $term_links ) { $result = array(); $exclude_cateogories = array( 'some category', 'another category', 'third category' ); foreach ( $term_links as $link ) { foreach ( $exclude_cateogories as $category ) { if ( stripos( $link, $category ) !== false ) continue 2; } $result[] = $link; } return $result; } );Simple correct
cateogorytocategory.March 23, 2021 at 6:57 am #1706652Tobias
Hey Elvin,
even if i corrected $exclude_cateogories to $exclude_categories it is still not working. The first link to the tag version of my problem works btw, but not with the categories code.
Can you check it again?
Sorry for making so much trouble about this little problem, thanks for your help.
March 23, 2021 at 8:14 am #1706767Tom
Lead DeveloperLead DeveloperHi there,
Can you share the full code you’re using now?
March 23, 2021 at 8:22 am #1706785Tobias
this one:
add_filter( "term_links-post_category", function( $term_links ) { $result = array(); $exclude_categories = array( 'my-category' ); foreach ( $term_links as $link ) { foreach ( $exclude_categories as $category ) { if ( stripos( $link, $category ) !== false ) continue 2; } $result[] = $link; } return $result; } );March 23, 2021 at 8:25 am #1706789Tom
Lead DeveloperLead DeveloperHave you updated
my-categoryto the actual slug of the category you want to exclude?March 23, 2021 at 8:28 am #1706793Tobias
yes, I tried every combination My Category, my category and my-category as placeholder for my category names. Still not 100% what to enter there, name or url-slug and what todo if you have two words. Maybe there could be error from my side, otherwise I don’t know 🙁
March 23, 2021 at 5:15 pm #1707233Elvin
StaffCustomer SupportIf I may suggest another way of doing this:
I believe
pre_get_postsshould be able to help you with this:Try this snippet:
add_filter('pre_get_posts', 'excludeCat'); function excludeCat($query) { if ( $query->is_home || $query->is_archive) { $query->set('cat', '-1'); } return $query; }March 23, 2021 at 5:24 pm #1707243Tobias
this one has no effect too. I tested it in different variations, I also deactivated all plugins. The only thing that worked so far was the term_links-post_tag filter.
Do I need to have any gp premium modules activated for the term_links-post_category filter?
Thanks for checking this for me. Best regards
March 23, 2021 at 7:29 pm #1707303Elvin
StaffCustomer SupportThat’s quite strange.
I made sure I tested the code I’ve provided you and it works on my end. You can see it in action here:
https://dev-generate-press.pantheonsite.io/What the code does is, it removes posts with the category with a cat id of
1on blog page and archives. (we can include search with|| $query->is_searchwithin the condition).You can change
-1to any-nwherenis the ID of the category you want to be excluded on the post loop.Example: exclude the category Movies with a cat ID of 33 on blog index, search and any archives.
add_filter('pre_get_posts', 'excludeCat'); function excludeCat($query) { if ( $query->is_home || $query->is_archive || $query->is_search) { $query->set('cat', '-33'); //33 is category Movies ID. } return $query; }Are the posts you’re pertaining to custom post types? This won’t work as-is on custom post types.
March 24, 2021 at 5:05 am #1707709Tobias
Hey Elvin,
my posts are standard post and i am not far away from a basic installation with a few changes made and plugins installed.
the new code does create a critical error on my page and I think it is not the right code I am looking for. I don’t want to hide the posts in my category, I want to hide the link to the category under the title (Every post hast two categories and I like to display one category for each post in the archive and home pages. like this: https://snipboard.io/cq8g9C.jpg
I think the first post from dave was good: https://generatepress.com/forums/topic/exclude-a-specific-tag-from-the-output-of-the-tag-list/#post-1071610
having this code for categories would do the job, but I am not able to get it running.
-
AuthorPosts
- You must be logged in to reply to this topic.