I want to show the related (archive) month and year for a post next to its contents, i.e. "Archived in January 2012". Any help / directions is greatly appreciated.
2 Answers
Use following code after the loop:
$month_num = get_the_date('n');
$month_txt = get_the_date('F');
$year = get_the_date('Y');
// change 'posts_per_page' to any value you need, '-1' means 'all'
query_posts( 'posts_per_page=-1&monthnum='.$month_num.'&year='.$year );
echo '<p class="">Archived in '.$month_txt.', '.$year.'</p>';
echo '<ul>';
while ( have_posts() ) : the_post();
echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
endwhile;
echo '</ul>';
wp_reset_query();
It's not tested, but you got the idea.
Use get_the_time()
, get_month_link()
and get_year_link()
:
function wpse_58787_archive_link()
{
$Y = get_the_time( 'Y' ); // year
$m = get_the_time( 'm' ); // month as number
$F = get_the_time( 'F' ); // month as word
return 'Archived in <a href="' . get_month_link( $Y, $m ) . '">' . $F . '</a>
<a href="' . get_year_link( $Y ) . '">' . $Y . '</a>';
}
print wpse_58787_archive_link();