Skip to content

Instantly share code, notes, and snippets.

@dsmy
Created July 31, 2018 02:55
Show Gist options
  • Save dsmy/377fc764802242b88e23fae8cf48192f to your computer and use it in GitHub Desktop.
Save dsmy/377fc764802242b88e23fae8cf48192f to your computer and use it in GitHub Desktop.
Custom Blog meta with author name outside of WordPress loop
if ( ! function_exists( 'atmosphere_posted_on' ) ) :
/**
* Prints HTML with meta information for the current post-date/time and author.
*/
function atmosphere_posted_on() {
global $post;
$author_id = $post->post_author;
$author = get_the_author_meta('display_name', $author_id);
$time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';
if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
$time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time><time class="updated" datetime="%3$s">%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() )
);
$posted_on = sprintf(
/* translators: %s: post date. */
esc_html_x( 'Posted on %s', 'post date', 'gutenbergtheme' ),
'<a href="' . esc_url( get_permalink() ) . '" rel="bookmark">' . $time_string . '</a>'
);
$byline = sprintf(
/* translators: %s: post author. */
esc_html_x( 'by %s', 'post author', 'gutenbergtheme' ),
'<span class="author vcard">' . esc_html( get_the_author_meta('display_name', $author_id) ) . '</span>'
);
echo '<span class="posted-on">' . $posted_on . '</span><span class="byline"> ' . $byline . '</span>'; // WPCS: XSS OK.
}
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment