[Support request] Show categories only on archive pages

Home Forums Support [Support request] Show categories only on archive pages

Home Forums Support Show categories only on archive pages

Viewing 15 posts - 1 through 15 (of 22 total)
  • Author
    Posts
  • #1366430
    Jon

    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
            );
        }
    } );
    #1366880
    Leo
    Staff
    Customer Support

    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 🙂

    #1366886
    Jon

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

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

    #1367080
    Leo
    Staff
    Customer Support

    We can actually use this filter now:
    https://docs.generatepress.com/article/generate_header_entry_meta_items/

    So remove your existing snippet and replace it with this one:

    add_filter( 'generate_header_entry_meta_items', function( $items ) {
        if ( is_front_page() && is_home() ) {
            $items = array(
                'categories',
            );
        } return $items;
    } );
    #1367464
    Jon

    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

    #1367832
    Leo
    Staff
    Customer Support

    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?

    #1367850
    Jon

    Hey Leo,

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

    #1367854
    Leo
    Staff
    Customer Support

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

    #1367860
    Jon

    Sure, cache is now cleared and deactivated.

    #1368079
    Leo
    Staff
    Customer Support

    I’m still seeing WP Rocket enabled.

    Can you double check?

    #1368495
    Jon

    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.

    #1368760
    Leo
    Staff
    Customer Support

    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

    #1368778
    Jon

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

    I’ve removed the CSS.

    #1369073
    Tom
    Lead Developer
    Lead Developer

    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' ] );
    } );
    #1369529
    Jon

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

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