In the WordPress context, a byline is a section that displays information about a given post. It’s usually located somewhere near the post title and displays the post’s publish date, author, and other secondary information.
While bylines vary from site to site, there’s no doubt you’ll build a byline or two if you’re building WordPress websites regularly.
There’s something you should know that can easily be overlooked in your testing environment. It’s all about one of the functions used to display the date in WordPress and what you can do to avoid some strange behavior.
Using the_date() Function to Display Post Dates
Let’s get straight to the point.
the_date() will only display the date once per page on your website. That means if two posts are published on the same day and both posts appear on the same page, such as a blog home, search results, or an archive page, only the first of those two posts will display a date.
Here’s a special note on the description of the_date() function straight from the WordPress Codex.
What happens to the second date, you ask? Well… it simply doesn’t display. Depending on your byline design, it can simply leave a gaping hole where the date should be. That’s no good.
Here’s what three posts that share the same date look like on the blog home when using
the_date() to display the post date.
Notice the missing dates on the bottom two posts. Just like that… there’s no warning or placeholder. You simply lose your dates. Don’t let that happen.
Using get_the_date() Function to Display Post Dates
Instead of using
the_date(), which some may think is the easier solution considering it returns a string of the post date, you should use
get_the_date() will return the date of a given post. However, it will not display it on the screen. It simply gives you the date value and you do with it what you please.
Since the goal is to display the date, you can just echo
get_the_date() to the screen where you would have normally just placed
the_date() to do all the work for you.
<span class="entry-date"><?php echo get_the_date(); ?></span>
<span class="entry-date"><?php the_date(); ?></span>
get_the_date() will display any number of times on a single page so you don’t have to worry about missing dates. Some of you may be have development environments that do not have multiple posts published on the same date. That makes this easy to overlook.
Make sure you 1) build this scenario into your test content and 2) use the more flexible function in your themes. WPTest is my favorite testing content. It will create this scenario for you.
Don’t Forget About Escaping and Echoing
Just as an additional tip, remember to adjust your HTML escaping functions if you’re using them.
There’s a chance that if you used
the_date(), you may have been using it like so:
<?php esc_html( the_date() ); ?>
If so, and you change your date function to the more flexible function that only returns the value and doesn’t echo it, you now need to echo the date along with escaping.
<?php echo esc_html( get_the_date() ); ?>
Happy coding. 🙂