[Support request] Display categories on the top of the blogpost?

Home Forums Support [Support request] Display categories on the top of the blogpost?

Home Forums Support Display categories on the top of the blogpost?

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #1233098
    Janne

    Hi.
    Currently the categories are displayed at the end of the post. I want to display them below the title.
    Is it possible to display the parent category on the top of the blogpost?

    #1233361
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    Are you wanting to display it inline with your other meta items? If so, where would you like them to display? After everything else?

    Let me know πŸ™‚

    #1233724
    Janne

    We want it below the title of the blogpost. Is it also possible to only show the parent category, not all?

    #1234238
    Tom
    Lead Developer
    Lead Developer

    What if there isn’t a parent?

    Should be possible using a Hook Element, just need to figure out the code we’ll need πŸ™‚

    #1234410
    Janne

    If there isnt a parent, we dont want anything to be shown.

    #1234602
    Tom
    Lead Developer
    Lead Developer

    You could try this:

    add_action( 'generate_after_entry_title', function() {
        $cat = get_the_category();
        if ( isset( $cat[0]->parent ) ) {
            var_dump($cat[0]->parent);
            echo '<div class="parent-category">' . get_cat_name($cat[0]->parent) . '</div>';
        }
    } );

    Let me know πŸ™‚

    #1235114
    Janne

    Dont lookslike its working. Are now using this code, and that is working. But this displays all categories:

    add_action( 'generate_after_entry_title', 'lh_add_cat_below_title' );
    function lh_add_cat_below_title() {
        if (is_single() ) {
            $categories_list = get_the_category_list( _x( ', ', 'Used between list items, there is a space after the comma.', 'generatepress' ) );
            $categories_list = sprintf( '<span class="cat-links"><span class="screen-reader-text">%1$s </span>%2$s</span>',
                _x( 'Categories', 'Used before category names.', 'generatepress' ),
                $categories_list
            );
        }
    
        ?>
        <div class="entry-meta">
    	<?php echo $categories_list; ?>
        </div>
        <?php
    }

    Both code snippets are running on my site now.

    #1235276
    Janne

    Now I understand what happened, your code added the categories on the blog page, not on the blogpost. It created a mess on my site, but removed your code now.

    #1235682
    Tom
    Lead Developer
    Lead Developer
    #1236040
    Janne

    Nothing happened. Removed my code, and loaded your code, and nothing showed on the blogpost page. Tried running both my and your code, and then all the categories showed. Now both code snippets are active on my site.

    #1236421
    Tom
    Lead Developer
    Lead Developer

    Can you link me to your site so I can take a look?

    #1237282
    Janne

    Have added a link in the first post.

    #1237967
    Tom
    Lead Developer
    Lead Developer

    Can you try the updated code?: https://generatepress.com/forums/topic/display-categories-on-the-top-of-the-blogpost/#post-1234602

    I added a debugging line in there to see if we have a parent object.

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