Site logo

Archived topic

Add a second line for updated post

12 replies · Started by Zad on December 26, 2018

Viewing posts 1–13 of 13

So I currently have the meta entry formatted with this PHP code

add_filter( 'generate_post_author', '__return_false' );
add_filter( 'generate_show_comments', '__return_false' );
add_filter( 'generate_post_date_output', 'tu_fancy_byline' );
function tu_fancy_byline( $date ) {
	printf( ' <span class="byline">%1$s</span>',
		sprintf( '<span class="author vcard" itemtype="http://schema.org/Person" itemscope="itemscope" itemprop="author"><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() )
		)
	);

	if ( ! is_single() && ! post_password_required() && ( comments_open() || get_comments_number() ) ) {
		echo '<span class="comments-link">';
			comments_popup_link( __( 'Leave a comment', 'generatepress' ), __( '1 Comment', 'generatepress' ), __( '% Comments', 'generatepress' ) );
		echo '</span>';
	}

	echo $date;
}

and this CSS

/* Styles the entry meta */
.byline,
.comments-link,
.posted-on {
	display: inline-block;
}

.comments-link,
.posted-on {
	border-left: 1px solid #ddd;
	padding-left: 10px;
        padding-right: 10px;
        padding-top: 0px;
        padding-bottom: 0px;
        margin-left: 10px;
        margin-right: 10px;
        margin-top: 0px;
        margin-bottom: 0px;
}

.comments-link:before {
	display: none;
}

h2.entry-title {
    margin-bottom: 20px;
}

/* Updated Post Date */
.posted-on .updated {
    display: inline-block;
}

.posted-on .updated + .entry-date {
    display: none;
}

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

This gives me: Author | Date posted on

However, I was wondering if it was possible to add a second line beneath that which indicates when it was last updated. So it would be:

Author | Date Posted
Last Updated:

Hi there,

Try replacing:

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

with

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

Let me know :)

Unfortunately, that didn't work. Here's what made some difference but still not complete. I updated my PHP code to this,

add_filter( 'generate_post_author', '__return_false' );
add_filter( 'generate_show_comments', '__return_false' );
add_filter( 'generate_post_date_output', 'tu_fancy_byline' );
function tu_fancy_byline( $date ) {
	printf( ' <span class="byline">%1$s</span>',
		sprintf( '<span class="author vcard" itemtype="http://schema.org/Person" itemscope="itemscope" itemprop="author"><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() )
		)
	);

	if ( ! is_single() && ! post_password_required() && ( comments_open() || get_comments_number() ) ) {
		echo '<span class="comments-link">';
			comments_popup_link( __( 'Leave a comment', 'generatepress' ), __( '1 Comment', 'generatepress' ), __( '% Comments', 'generatepress' ) );
		echo '</span>';
	}
	
  $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_string . '<time class="updated" 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() )
	);

	echo sprintf( '<span class="posted-on">%1$s</span>', // WPCS: XSS ok, sanitization ok.
		sprintf( '<a href="%1$s" title="%2$s" rel="bookmark">%3$s</a>',
			esc_url( get_permalink() ),
			esc_attr( get_the_time() ),
			$time_string
		)
	);
  
} 

and added this bit of CSS

/* Updated Post Date */
.posted-on .updated {
    display: block;
}

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

.posted-on .published:before {
    content: "Published on: "
}

What I'm still hoping to do is get rid of the modified date on the archive pages because now there are two dates showing there and I'd also like to get rid of the hyperlinks from the dates

For some reason, the number of comments don’t seem to show up either in the entry meta. Not sure what the problem is, as I have tried turning off all the PHP and plugins and my commenting system, and the only time the comments show up in the entry meta is when I switch to the twenty19 theme. I’ve also uninstalled and reinstalled both generate press and the child themes along with the premium plugin. No dice. Comments don’t show up in the entry meta for individual posts.

So the only problem now is that comments don't show up in single post entry meta?

That's by default.

Yes, but isn't it supposed to do with the entry meta code above? And I'm also trying to remove the hyperlinks from the dates and the "updated date" from the archive pages.

To have the comments link on single posts and remove the date/link, you could do this instead:

add_filter( 'generate_post_author', '__return_false' );
add_filter( 'generate_show_comments', '__return_false' );
add_filter( 'generate_post_date_output', 'tu_fancy_byline' );
function tu_fancy_byline( $date ) {
	printf( ' <span class="byline">%1$s</span>',
		sprintf( '<span class="author vcard" itemtype="http://schema.org/Person" itemscope="itemscope" itemprop="author"><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() )
		)
	);

	if ( ! post_password_required() && ( comments_open() || get_comments_number() ) ) {
		echo '<span class="comments-link">';
			comments_popup_link( __( 'Leave a comment', 'generatepress' ), __( '1 Comment', 'generatepress' ), __( '% Comments', 'generatepress' ) );
		echo '</span>';
	}
	
  $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_string . '<time class="updated" 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() )
	);

	echo sprintf( 
            '<span class="posted-on">%1$s</span>', // WPCS: XSS ok, sanitization ok.
            $time_string
	);
  
} 

Thanks Tom! That works great. For the archive pages with the lists of post, I was wondering if there was a way to remove the modified date, so that posts only have the Published date next to them. Thanks again!

Yes, in the archive pages, both dates show, though I only want the published date. I have some CSS to include the modified date and the published date for single blog posts,

/* Styles the entry meta */
.byline,
.comments-link,
.posted-on {
	display: inline-block;
}

.byline {
	padding-left: 10px;
        padding-right: 0px;
        padding-top: 0px;
        padding-bottom: 0px;
        margin-left: 10px;
        margin-right: 0px;
        margin-top: 0px;
        margin-bottom: 0px;
}

.comments-link {
	border-left: 1px solid #ddd;
	padding-left: 10px;
        padding-right: 0px;
        padding-top: 0px;
        padding-bottom: 0px;
        margin-left: 10px;
        margin-right: 0px;
        margin-top: 0px;
        margin-bottom: 0px;
}

.posted-on {
	border-left: 1px solid #ddd;
	padding-left: 10px;
        padding-right: 0px;
        padding-top: 0px;
        padding-bottom: 0px;
        margin-left: 10px;
        margin-right: 0px;
        margin-top: 0px;
        margin-bottom: 0px;
        text-align: left;
}

.comments-link:before {
	display: none;
}

h2.entry-title {
    margin-bottom: 20px;
}

/* Updated Post Date */
.posted-on .updated {
    display: block;
}

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

.posted-on .published:before {
    content: "Posted: "
}

Try replacing this:

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

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

.posted-on .published:before {
    content: "Posted: "
}

With this:

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

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

.single .posted-on .published:before {
    content: "Posted: "
}

Thanks Tom, that worked perfectly! Appreciate all your help.

You're welcome :)

This archived topic is closed to new replies.