Site logo

Archived topic

Show Categories In Search Result But Hiding It Everywhere Else

5 replies · Started by Joezer on June 12, 2020

Viewing posts 1–6 of 6

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.

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;
}

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.

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

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;
} );

Can you link me to a VIP post/archive?

This archived topic is closed to new replies.