Site logo

Archived topic

Author, date published and date modified order

3 replies · Started by Igor on November 30, 2019

Viewing posts 1–4 of 4

Hi guys,

First, I would like to thank you for providing such amazing support on this forum, so far every problem I had was already solved here, each with a handy css or php code ready to just copy and paste. Amazing!

However, now I have a problem I wasn't able to solve using existing advice. I would like to have following format of byline for my posts:

author - date published (updated: date modified)

On top of this I don't want either author or those dates to be links.

So far, using code snippets and css already shared on this forum I was able to achieve this:

date published (updated: date modified) by author

Here are the PHP code snippets I'm using for this:

add_filter( 'generate_post_date_output', function( $output ) {
	$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( '%3$s',
			esc_url( get_permalink() ),
			esc_attr( get_the_time() ),
			$time_string
		)
	);
} );
add_filter( 'generate_post_author_output','tu_no_author_link' );
function tu_no_author_link() {
	printf( ' <span class="byline">%1$s</span>',
		sprintf( '<span class="author vcard" itemtype="http://schema.org/Person" itemscope="itemscope" itemprop="author">%1$s <span class="fn n author-name" itemprop="name">%4$s</span></span>',
			__( 'by','generatepress'),
			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() )
		)
	);
}

And CSS:

.posted-on .updated {
    display: inline-block;
		margin-left:10px;
}

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

.posted-on .updated:after {
    content: ") "
}

So it's almost as I want it, the only problem which remains is to have the author at the beginning (and instead "by Author", just "Author").

Hi there,

Try adding this as well:

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

add_filter( 'generate_inside_post_meta_item_output', function( $output, $item ) {
    if ( 'author' === $item ) {
        return '';
    }

    return $output;
}, 15, 2 );

Let me know :)

It works! Thank you!

You're welcome :)

This archived topic is closed to new replies.