• Anyone know of a PHP snippet which I could place into the template which would have a link to a post on the same day, but a year prior? Something to the effect of “On this day in 2011, 2010” etc?

    Bonus if the code is “smart” and grays out the year if no post is available.

Viewing 2 replies - 1 through 2 (of 2 total)
  • https://codex.www.ads-software.com/Class_Reference/WP_Query#Time_Parameters

    example (this links to the date archive one year ago):

    <?php $on_this_day = get_posts('year='.(date('Y')-1).'&monthnum='.date('n').'&day='.date('j'));
    if( $on_this_day ) {
    	echo '<a class="on-this-day" href="'.get_day_link( (date('Y')-1), date('m'), date('d')).'" title="On This Day in '.(date('Y')-1).'">';
    	echo 'On this day in '.(date('Y')-1);
    	echo '</a>';
    } else {
    	echo '<span class="on-this-day">'.'On this day in <span>'.(date('Y')-1).'</span></span>';
    } ?>

    https://codex.www.ads-software.com/Function_Reference/get_day_link

    if you want to link to just one (random) post, try and use:

    <?php $on_this_day = get_posts('year='.(date('Y')-1).'&monthnum='.date('n').'&day='.date('j').'&posts_per_page=1&orderby=rand');
    if( $on_this_day ) {
    	echo '<a class="on-this-day" href="'.get_permalink( $on_this_day[0]->ID ).'" title="On This Day in '.(date('Y')-1).'">';
    	echo 'On this day in '.(date('Y')-1);
    	echo '</a>';
    } else {
    	echo '<span class="on-this-day">'.'On this day in <span>'.(date('Y')-1).'</span></span>';
    } ?>

    the ‘greying out’ of the year should be possible with css formatting of .on-this-day span { ... }

    Thread Starter drsprite

    (@drsprite)

    Perfect, thank you! I think I can use this to make it go back many years.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘"On this day" PHP code for template?’ is closed to new replies.