0

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.

0

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.

1
  • Great, thanks guys! Wasn't familiar of get_the_date(). Jul 23 '12 at 11:46
0

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();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.