Site logo

[Support request] Hide specific categories on front and archive pages in post meta

Home Forums Support [Support request] Hide specific categories on front and archive pages in post meta

Home Forums Support Hide specific categories on front and archive pages in post meta

Viewing 15 posts - 1 through 15 (of 19 total)
  • Author
    Posts
  • #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.jpg

    should be like this:
    https://snipboard.io/cq8g9C.jpg

    sorry for bad explanation

    #1705291
    David
    Staff
    Customer Support

    Hi there,

    Tom provides a solution here for Post Tags:

    https://generatepress.com/forums/topic/exclude-a-specific-tag-from-the-output-of-the-tag-list/#post-1071610

    The same principles should apply – but instead of:

    term_links-post_tag filter

    you would use the:

    term_links-post_category filter

    #1705323
    Tobias

    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!

    #1705345
    David
    Staff
    Customer Support

    Try 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.

    #1705481
    Tobias

    Hey Dave,

    it is not working, even if I corrected cateogory and cateogories 🙁
    can you check it again for me?

    Thanks and best regards

    #1705862
    Elvin
    Staff
    Customer Support

    Hi 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 cateogory to category.

    #1706652
    Tobias

    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.

    #1706767
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    Can you share the full code you’re using now?

    #1706785
    Tobias

    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;
    } );
    #1706789
    Tom
    Lead Developer
    Lead Developer

    Have you updated my-category to the actual slug of the category you want to exclude?

    #1706793
    Tobias

    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 🙁

    #1707233
    Elvin
    Staff
    Customer Support

    If I may suggest another way of doing this:

    I believe pre_get_posts should 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;
    }
    #1707243
    Tobias

    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

    #1707303
    Elvin
    Staff
    Customer Support

    That’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 1 on blog page and archives. (we can include search with || $query->is_search within the condition).

    You can change -1 to any -n where n is 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.

    #1707709
    Tobias

    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.

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