Site logo

Archived topic

Show categories only on archive pages

21 replies · Started by Jon on July 17, 2020

Viewing posts 1–15 of 22

I'm using the below code to display post categories below the title on my blog archive pages.

It works great, but how to I prevent the categories from also displaying on the single post pages?


add_filter( 'generate_show_categories', '__return_false' );

add_action( 'generate_after_entry_title', function() {
    $categories_list = get_the_category_list( ' | ' );

    if ( $categories_list ) {
        printf( '<div class="cat-links"><span class="screen-reader-text">%1$s </span>%2$s</div>',
            esc_html_x( 'Categories', 'Used before category names.', 'generatepress' ),
            $categories_list
        );
    }
} );

Hi there,

There should be an easier way to do this now.

Any chance you can link us to the site in question?

You can edit the original topic and use the private URL field.

Let me know :)

Hi Leo, thanks very much, I've added the URL.

I currently have the categories on the single post page hidden using CSS.

Hi Leo, thanks for that.

The categories are now displaying on the correct pages, but they're back in their regular spot, how do I place them underneath the entry title again?

Cheers,

Jon

Hmm my snippet should make it show right below the post title - just tested as well.

Are you adding any other functions for the meta?

Hey Leo,

Nope, nothing else, just the code snippet you suggested. The categories show below the excerpt.

Can you clear and disable WP Rockets and any other caching plugins or server cache for now?

Sure, cache is now cleared and deactivated.

I'm still seeing WP Rocket enabled.

Can you double check?

I re-activated it again before going to bed last night (I'm based in the UK), as it makes quite a difference in the speed of the site.

It's deactivated again now.

Thanks.

You are using Code Snippets?

Can you make sure the snippet is activated?

I'm having a tough time seeing why it's not working.

Try removing this CSS as well:
https://www.screencast.com/t/7zXoj4Zam3

Yes I'm using Code Snippets, the snippet is activated.

I've removed the CSS.

Hi there,

Try this instead:

add_filter( 'generate_header_entry_meta_items', function( $items ) {
    if ( is_archive() || is_home() ) {
        $items[] = 'categories';
    }

    return $items;
} );

add_filter( 'generate_footer_entry_meta_items', function( $items ) {
    return array_diff( $items, [ 'categories' ] );
} );

Hi Tom, no joy I'm afraid, the categories aren't showing at all now.

This archived topic is closed to new replies.