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:

<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>

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 *

Tell us about your project.

We'd love to hear what you're working on.

Work with us

Hi. We are Spigot. Telemarketers pronounce it Spy-got.

We are a Park City, Utah based web design shop that excels at building custom websites powered by WordPress and WooCommerce. We love content - content strategy, content curation, content soup...????Have a look around the site. As you browse you'll find useful navigation links in the sidebar on the left. We hope you find them useful anyway... Please let us know what you think, shoot us a message here.