Site logo

Archived topic

Published or Updated Text and Icon for Date

19 replies · Started by Rohan Verma on August 5, 2020

Viewing posts 1–15 of 20

Hello.

I want to achieve both the published and updated text for the article date on a single post.

Currently, it's only showing the updated text for updated article.

1. Similarly, I want published text for the single time published article.
2. And I have followed a PHP code for getting date icon on other topic. But the icon is showing after the date. How can I place it before published/updated text?

Hi there,

This article should help: https://docs.generatepress.com/article/show-the-updated-post-date/

Your CSS would look like this:

.posted-on .updated {
    display: inline-block;
}

.posted-on .updated:before {
    content: "Last Updated ";
}

.posted-on .entry-date:before {
    content: "Published ";
}

Which code have you added to add the icon? Where's the icon coming from exactly? A custom SVG?

According to your given code, the date tag shows double: https://prnt.sc/tuoib7

I don't want like this. There will be only one tag. Not both published and updated.

Either it will show Published for the fresh articles or it will show Updated for the updated articles.

Hope you understand.

Hi there,

in this article:
https://docs.generatepress.com/article/generate_post_date_output/#only-show-updated-date

The code has two $time_string variables - only one will be displayed. The first is for the published date, change it to this:

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

The second line change to this:

$time_string = '<time class="entry-date updated-date" datetime="%3$s" itemprop="dateModified">Last Updated %4$s</time>';

Hi David, But your given two strings are similar to that link.

So, what to change for published and updated?

The code changes i provided above include the Published and Last Updated labels.
So your updated code would look like this:

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

    $updated_time = get_the_modified_time( 'U' );
    $published_time = get_the_time( 'U' ) + 86400;

    if ( $updated_time > $published_time ) {
        $time_string = '<time class="entry-date updated-date" datetime="%3$s" itemprop="dateModified">Last Updated %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 );

What other post meta related functions have you added to your site ?

Remove link from date.
Show author avatar beside the name.
Author box hook element.

There are a bunch of css codes but I don't know any of them conflicting or not. Should I share with you the css codes...

Its not the CSS.
It will be either the remove link and/or show avatar.
Can you share them here

Show avatar:

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<a href="%1$s" title="%2$s" rel="author"><span class="author-name" itemprop="name">%3$s</span></a></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',
        'comments-link',
        'date',
    );
} );

Remove link:

add_filter( 'generate_post_date_output','tu_remove_date_link', 10, 2 );
function tu_remove_date_link( $output, $time_string ) {
printf( '<span class="posted-on">%s</span>',
$time_string
);
}

Remove the 'remove link' function

Thanks. It's working.

1. But how to add : symbol after published or updated text?
2. How can I make the bold font of both author and published/updated text?

This archived topic is closed to new replies.