Show the date and author of posts in the WordPress theme

In this post, I'll show you how to display the date and author of a post in the WordPress theme. The post date and post author are output with structured data in the hAtom microformat for better readability in search engines.

To display the date and author, you can create your own template functions in the functions.php of the theme. The functions can then be used in several places (e.g. archives, individual contributions) and are more easily adaptable with child themes.

Show date in WordPress theme

We can use the WordPress function get_the_date () to output the post date :

 echo get_the_date();

A date format could be passed as a parameter, but this is not recommended.

Without explicit specification, WordPress uses the format set in the WordPress settings. Users can change the date format themselves under Settings → General .

To ensure that it works correctly, make sure that the function is called within the WordPress loop.

Output post date for search engines in the hAtom standard

You could end the tutorial at this point. Finally, the output of get_the_date () is enough to display the date in the WordPress theme.

However, it is advisable to provide the post date as structured data for search engines.

With structured data, Google & Co can better understand the content and expand the search results with this data. So the Google search only shows the date and author of the posts if these details are correctly identified by the theme.

WordPress uses microformats with the hAtom specification to structure the page content. For this purpose, the class hentry is added to all WordPress posts by default using the post_class () function.

For theme developers, when outputting the date, make sure that the classes published and updated are used there. These correspond to the hAtom standard and can therefore be read by Google.

A correct creation of the date with the required hAtom classes can be found in the Underscores Starter Theme, for example:

 $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() ) );

If the date of publication differs from the date of the last update of the article, two dates with published and updated class are output.

The second date can be hidden for visitors with some CSS code:

 .updated:not(.published) { display: none; }

This approach is used in TwentySeventeen, among others.

Template function for the output of the contribution date

In addition to adding the hAtom classes, it is a good idea to link the date to the URL of the post. This has now established itself as the standard in most WordPress themes.

In addition, some text such as "Published on" can be displayed.

I have provided the complete template function as a Github Gist:

With the if query with function_exists () at the beginning, we create a pluggable function. This means that the template function can be overwritten in a child theme if desired.

Show author in WordPress theme

We handle the author's output with the WordPress function get_the_author () .

 echo get_the_author();

The name of the author should be linked to his author archive, which shows all contributions of the author.

We get the URL to the author's archive with get_author_posts_url () .

 get_author_posts_url( get_the_author_meta( 'ID' ) );

Author of the post in hCard

We also want to provide structured content with microformats for the display of the post author so that search engines have access to it.

hAtom prescribes the class author and the use of the hCard standard:

 $author = sprintf( '<span class="author vcard"><a class="url fn n" href="%1$s" rel="author">%2$s</a></span>', esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ), esc_html( get_the_author() ) );

hCard requires the vcard and url and fn classes for the link.

Template function for the output of the article author

Also for the author I have again packed the complete template function into a Github Gist:

Show the date and author of a post in the WordPress theme

The template functions can now be used anywhere within the WordPress loop to display the date and author of the post.

If desired, a single template function can also be created, which outputs all metadata of the article:

 /** * Display date and author of posts */ function theme_slug_entry_meta() { $postmeta = theme_slug_entry_date(); $postmeta .= theme_slug_entry_author(); echo '<div class="entry-meta">' . $postmeta . '</div>'; }

In addition to the date and author, the category, keywords and comments can also be output there.

I will soon show you how to best spend it in another post.