Site logo

Archived topic

Customize entry meta author name, date, category

25 replies · Started by Tan on March 30, 2020

Viewing posts 1–15 of 26

Hi there,
I want to customize entry meta in order: ava author, author name, published date/last updated date, category. Could you help me?
Snippet I used:

add_filter( 'generate_post_author_output', function() {
    return sprintf( ' <span class="byline">%1$s</span>',
        sprintf( '<span class="author vcard" itemtype="http://schema.org/Person" itemscope="itemscope" itemprop="author">%4$s<span class="author-name" itemprop="name">%3$s</span></span>',
            esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
            esc_attr( sprintf( __( 'View all posts by %s', 'generatepress' ), get_the_author() ) ),
            esc_html( get_the_author() ),
            get_avatar( get_the_author_meta( 'ID' ) )
        )
    );
} );

add_filter( 'generate_header_entry_meta_items', function() {
    return array(
        'author',
        'date',
    );
} );

add_filter( 'generate_footer_entry_meta_items', function( $items ) {
    return array(
        'date',
        'categories',
    );
} );

Hi there,

Do you want all of those below the title so it looks like this?:

avatar - author name | published date | updated date | categories

Let me know :)

I want it like:
avatar (no dash between) author name | Published date (or last updated date when I change something) | categories
Thank you for your reply, Tom :)

Try this:

add_filter( 'generate_post_author_output', function() {
    return sprintf( ' <span class="byline">%1$s</span>',
        sprintf( '<span class="author vcard" itemtype="http://schema.org/Person" itemscope="itemscope" itemprop="author">%4$s<span class="author-name" itemprop="name">%3$s</span></span>',
            esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
            esc_attr( sprintf( __( 'View all posts by %s', 'generatepress' ), get_the_author() ) ),
            esc_html( get_the_author() ),
            get_avatar( get_the_author_meta( 'ID' ) )
        )
    );
} );

add_filter( 'generate_post_date_output', function( $output, $time_string ) {
    $time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">%2$s</time>';

    if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
        $time_string = '<time class="entry-date updated-date" datetime="%3$s" itemprop="dateModified">%4$s</time>';
    }

    $time_string = sprintf( $time_string,
        esc_attr( get_the_date( 'c' ) ),
        esc_html( get_the_date() ),
        esc_attr( get_the_modified_date( 'c' ) ),
        esc_html( get_the_modified_date() )
    );

    return sprintf( '<span class="posted-on">%s</span> ',
        $time_string
    );
}, 10, 2 );

add_filter( 'generate_header_entry_meta_items', function() {
    return array(
        'author',
        'date',
        'categories',
    );
} );

add_filter( 'generate_footer_entry_meta_items', '__return_null' );

Thank you so much.
The result like this: https://bit.ly/2R0SaHW
Now, it is showing both published date and updated date. I just want to show one of them. If I updated something it show updated date, if I didn't updated it show.
And I want to add some custom text before updated date like "Cập nhật vào".
Could you help me?
Thanks again :)

Hmm, it shouldn't be showing the two dates according to the code I provided. Are you using any other custom function? Specifically, ones that are using the generate_post_date_output filter?

You can add text before the <time... HTML :)

Awesome. It deleted old snippet and it worked.
I want to add a custom text when published date is showed and another when updated date is showed. Could you show me the code?
Thank you so much.

Hi there,

in the code Tom provided you have two lines where the $time_string is generated - both shown below where i have added ADD YOUR TEXT HERE:

1. Published Date

$time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">ADD YOUR TEXT HERE %2$s</time>';

2. Updated Date

$time_string = '<time class="entry-date updated-date" datetime="%3$s" itemprop="dateModified">ADD YOUR TEXT HERE %4$s</time>';

Hi David,
Thank you for your help.
I test by adding new post and it shows the text for updated date, not published day :(

Can you share the full function you're using?

It's here:

add_filter( 'generate_post_author_output', function() {
    return sprintf( ' <span class="byline">%1$s</span>',
        sprintf( '<span class="author vcard" itemtype="http://schema.org/Person" itemscope="itemscope" itemprop="author">%4$s<span class="author-name" itemprop="name">%3$s</span></span>',
            esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
            esc_attr( sprintf( __( 'View all posts by %s', 'generatepress' ), get_the_author() ) ),
            esc_html( get_the_author() ),
            get_avatar( get_the_author_meta( 'ID' ) )
        )
    );
} );

add_filter( 'generate_post_date_output', function( $output, $time_string ) {
    $time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">Đăng ngày %2$s</time>';

    if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
        $time_string = '<time class="entry-date updated-date" datetime="%3$s" itemprop="dateModified">Cập nhật ngày %4$s</time>';
    }

    $time_string = sprintf( $time_string,
        esc_attr( get_the_date( 'c' ) ),
        esc_html( get_the_date() ),
        esc_attr( get_the_modified_date( 'c' ) ),
        esc_html( get_the_modified_date() )
    );

    return sprintf( '<span class="posted-on">%s</span> ',
        $time_string
    );
}, 10, 2 );

add_filter( 'generate_header_entry_meta_items', function() {
    return array(
        'author',
        'date',
        'categories',
    );
} );

add_filter( 'generate_footer_entry_meta_items', '__return_null' );

Have any of the posts only been published and not updated?

I published a new post for testing few minutes ago.
You can see it in my blog: https://eladiy.com/blog
It shows custom text "Cập nhật ngày". It should be showed "Đăng ngày" that follow the code.

Interesting.. Try this instead:

add_filter( 'generate_post_author_output', function() {
    return sprintf( ' <span class="byline">%1$s</span>',
        sprintf( '<span class="author vcard" itemtype="http://schema.org/Person" itemscope="itemscope" itemprop="author">%4$s<span class="author-name" itemprop="name">%3$s</span></span>',
            esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
            esc_attr( sprintf( __( 'View all posts by %s', 'generatepress' ), get_the_author() ) ),
            esc_html( get_the_author() ),
            get_avatar( get_the_author_meta( 'ID' ) )
        )
    );
} );

add_filter( 'generate_post_date_output', function( $output, $time_string ) {
    $time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">Đăng ngày %2$s</time>';

    if ( get_the_modified_time( 'U' ) > get_the_time( 'U' ) ) {
        $time_string = '<time class="entry-date updated-date" datetime="%3$s" itemprop="dateModified">Cập nhật ngày %4$s</time>';
    }

    $time_string = sprintf( $time_string,
        esc_attr( get_the_date( 'c' ) ),
        esc_html( get_the_date() ),
        esc_attr( get_the_modified_date( 'c' ) ),
        esc_html( get_the_modified_date() )
    );

    return sprintf( '<span class="posted-on">%s</span> ',
        $time_string
    );
}, 10, 2 );

add_filter( 'generate_header_entry_meta_items', function() {
    return array(
        'author',
        'date',
        'categories',
    );
} );

add_filter( 'generate_footer_entry_meta_items', '__return_null' );

I changed the code but it didn't work :(
Could you try again? Thank you.

This archived topic is closed to new replies.