Below is the code to link post dates to their archive pages in WordPress.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function prefix_link_post_dates() { | |
/* Get the year, month, and day of the current post. */ | |
$year = get_the_time('Y'); | |
$month = get_the_time('m'); | |
$day = get_the_time('d'); | |
$out = ''; | |
/* Add a link to the monthly archive. */ | |
$out.= '<a href="' . get_month_link($year, $month) . '" title="Archive for ' . esc_attr(get_the_time('F Y')) . '">' . get_the_time('F') . '</a>'; | |
/* Add a link to the daily archive. */ | |
$out.= ' <a href="' . get_day_link($year, $month, $day) . '" title="Archive for ' . esc_attr(get_the_time('F d, Y')) . '">' . $day . '</a>'; | |
/* Add a link to the yearly archive. */ | |
$out.= ', <a href="' . get_year_link($year) . '" title="Archive for ' . esc_attr($year) . '">' . $year . '</a>'; | |
return $out; | |
} | |
add_shortcode('post_date_links', 'prefix_link_post_dates'); |
Once this code snippet is added to your child theme’s functions.php file, you can use the shortcode [post_date_links] within the loop to display the post date. Each element of the post date (day, month, year) are now linked to their respective archive page (see example here).