Site logo

Archived topic

Change metadata order on blog archives

14 replies · Started by Joshua on June 27, 2020

Viewing posts 1–15 of 15

Hi,

I'd like to change where the metadata appears on the blog archive and category archive pages.

Currently the metadata (the category specifically) appears under the excerpt, for example: https://joshuamayo.com/blog/

I'd like the metadata to appears above the title instead (after the thumbnail).

I found this code that Tom posted on a different topic while trying to do research:


add_filter( 'generate_show_categories', '__return_false' );

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

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

But I have no idea how to alter it to make it work for my needs. Any help is greatly appreciated! :)

Joshua

Hi there,

first off you can use generate_header_entry_meta_items filter to set the entry meta to display your categories:

add_filter( 'generate_header_entry_meta_items', function( $items ) {
    if ( !is_single() ) {
        $items = array(
            'categories',
        );
    } return $items;
} );

Then you can unhook the entry meta and hook it in above the title with this snippet:

add_action( 'wp', function() {
    if (!is_single() ) {
        remove_action( 'generate_after_entry_title', 'generate_post_meta' );
        add_action( 'generate_before_entry_title', 'generate_post_meta', 15 );
    }
} );

Hey David,

Thank you for your reply as always!

I added this code to my functions.php and it doesn't seem to change the position of the categories.

I realize I may have been using the wrong terminology when describing the metadata. I don't actually think I want to move the metadata, but instead just the blog categories location to above the title (and below the thumbnail).

I did notice that when I go to customizer > layout > blog section and turn on the "Display post author" or "Display post date" that the category appears above the title, however, the category that is currently below the excerpt still displays. And when I try to turn that off by unchecking "Display post categories" both that category and the category that appears above the title disappear.

OK remove any of the code above and try this instead:

add_action( 'wp', function() {
    if (!is_single() ) {
        remove_action( 'generate_after_entry_content', 'generate_footer_meta', 10 );
        add_action( 'generate_before_entry_title', 'generate_footer_meta', 5 );
    }
} );

Hey David,

I removed the previous code and added the new code, but it still seems to not have an affect on the category position.

I also tried removing all the other code in my functions.php just in case there was something interfering with it, but to no avail.

That got rid of the category taxonomy completely. If I turn on "Display post date", oddly it appears in the right spot. However, when I turn on that option it also shows the category below the title. I left it turned on so you could see what I'm talking about.

Can you check that code again - i am not sure if the change i made updated

I updated the code, it almost works. It's still showing the double category. When I go to customizer > layout > blog and uncheck "Display post date", both categories disappear, even if " Display post categories" is checked.

well this is proving harder then it should be lol :)

Can you add the code so i can take a look ?

Sorry lol!

Code has been added again

Not your fault - i think i am having a bad meta day lol

So lets try this.
Remove that code.
Add this:

// Set entry meta to category only 
add_filter( 'generate_header_entry_meta_items', function( $items ) {
    if ( !is_single() ) {
        $items = array(
            'categories',
        );
    } return $items;
} );

// Return no footer meta
add_filter( 'generate_footer_entry_meta_items', function( $items ) {
    if ( !is_single() ) {
        $items = array(
            '',
        );
    } return $items;
} );
// Move entry meta above title
add_action( 'wp', function() {
    if (!is_single() ) {
        remove_action( 'generate_after_entry_title', 'generate_post_meta' );
        add_action( 'generate_before_entry_title', 'generate_post_meta', 15 );
    }
} );

And make sure you have the Date or Author meta set to display

That worked! Thank you so much David :)

haha awesome - great looking site :)

Thank you! :)

This archived topic is closed to new replies.