WordPress Post Date/Time & Author Meta Info

Take a look at the latest default WordPress theme and you’ll notice that you don’t see a call for the author, date or time of any given post called directly on the relevant template. Instead, this post meta data has been distilled into a handy function. That’s the way to go; DRY!

Defining the Function

To do something similar, crack open the functions.php. The first step in defining this function is to make it pluggable.

// make it pluggable
if ( ! function_exists( 'butter_post_meta' ) ) :
  function butter_post_meta() {
    // define the function
  }
endif;

With that in place, you can then define the markup as it should appear in the content/post templates whenever the function is called.

Use printf() to define the HTML markup of the meta info. Suppose you want to wrap the post date with a link to the permalink and the author’s name with a link to his/her author page. You also want to wrap the entire meta section in a div with CSS class “post-meta”. The above function would now look like this:

if ( ! function_exists( 'butter_post_meta' ) ) :
  function butter_post_meta() {
    printf( '<div class="post-meta"><a href="%1$s">%2$s</a> <a href="%3$s">%4$s</a></div>',
      esc_url( get_permalink() ),
      esc_html( get_the_date() ),
      esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
      get_the_author()
    );
  }
endif;

Using the function

Now, to output this meta information with the markup you defined you’d just make a call for the function.

<?php butter_post_meta(); ?>