[Support request] Show Categories In Search Result But Hiding It Everywhere Else

Home Forums Support [Support request] Show Categories In Search Result But Hiding It Everywhere Else

Home Forums Support Show Categories In Search Result But Hiding It Everywhere Else

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #1325900
    Joezer

    Hello awesome people of Generatepress!

    My setup is:
    Layout>Blog>Archives>Display post categories. I only want the category of the post to show up in search results. But in the category archive pages like site.com/category/cars, I want to hide the category links under the post titles, since it is pointing to the same page (site.com/category/cars) that is listed. Is there something that I could add in my functions to set it so that the post’s category links are not rendering in the category archives?

    I tried to display:none but it is still showing in the source code. Problem with this is that if a category archive is showing 20 post titles under that category, then the category meta link is showing 20 times for that. And since I very rarely use multiple categories for this site’s posts, I have 20 category meta link that is basically just pointing to the same page.

    #1326103
    David
    Staff
    Customer Support

    Hi there,

    you can use the Blog Options filter:

    https://docs.generatepress.com/article/option_generate_blog_settings/

    Something like this which should disable categories unless its the search page:

    add_filter( 'option_generate_blog_settings', 'db_custom_search_show_category' );
    function db_custom_search_show_category( $options ) {
        if ( !is_search() ) {
    
        	$options['categories'] = false;
    
        }
      
        return $options;
    }
    #1326209
    Joezer

    Hello David,

    It works! But the custom post types appearing in the search result doesn’t show the categories. It only shows the title while the regular posts works without problem. How do I make this so that it will also be included in the CPTs? I searched around and found this:

    add_filter( ‘generate_footer_meta_posts_types’, function( $types ) {
    $types[] = ‘pictures’;

    return $types;
    } );

    But I think this is not it..

    The CPTs are using the categories from WordPress core.

    Thanks, David.

    #1326825
    Tom
    Lead Developer
    Lead Developer

    If you’re using the core categories, then that code should work. You just need to change pictures to the name of your CPT.

    #1326935
    Joezer

    Hello Tom,

    I changed it, but it is still not showing. Kindly see this SS.. https://prnt.sc/szc9dk

    This is the code that I have in my functions.php

    add_filter( 'option_generate_blog_settings', 'db_custom_search_show_category' );
    function db_custom_search_show_category( $options ) {
        if ( !is_search() ) {
    
        	$options['categories'] = false;
    
        }
      
        return $options;
    }
    
    add_filter( 'generate_footer_meta_posts_types', function( $types ) {
        $types[] = 'vip';
    
        return $types;
    } );
    #1327593
    Tom
    Lead Developer
    Lead Developer

    Can you link me to a VIP post/archive?

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