Show the year only if it’s not the current year in WordPress

A recent project required that the post date show the year only if it wasn’t written in the current year.

Here’s how it’s done with WordPress:

[php]
<span class="date">

<?php if ( get_the_time(‘Y’) == date(‘Y’)) { ?>

<?php the_time(‘M j’) ?>

<?php } else { ?>

<?php the_time(‘M j, Y’) ?>

<?php } ?>

</span>
[/php]

This would of course go anywhere within a template that you’re outputting the date of a post. Or a custom post type.

Leave a Reply

Your email address will not be published. Required fields are marked *

Ready for a refreshing experience on your next website design?