[Resolved] How to Display Categories Below Post Title

Home Forums Support [Resolved] How to Display Categories Below Post Title

Home Forums Support How to Display Categories Below Post Title

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #520988
    Vish

    Currently the categories are displayed at the end of the post. I want to display them below the title as below:

    Example:
    GeneratePress is the best WordPress Theme Right Now (Title)
    Category 1, Category 2, Category 3
    March 16, 2018 by Vishnu

    The categories thing in the second line above is what I’m looking for. And I want this to happen only in single posts page. Not in Blog pages or normal pages.

    Also, please let me know where should I add what codes to achieve this.

    GeneratePress 2.0.2
    GP Premium 1.5.6
    #521135
    Leo
    Staff
    Customer Support

    Hi there,

    Try this PHP snippet:

    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
    }

    Adding PHP: https://docs.generatepress.com/article/adding-php/

    #1233095
    Janne

    Hi.
    The code is working fine, thank you. Is it possible to only display the parent category?

    #1233097
    Leo
    Staff
    Customer Support

    There is actually am easier solution now.

    Can you open a new topic for your question?

    Thanks πŸ™‚

    #1233141
    Janne

    Done πŸ™‚

    #1233142
    Leo
    Staff
    Customer Support
Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.