Title: &quot;On this day&quot; PHP code for template?
Last modified: August 20, 2016

---

# "On this day" PHP code for template?

 *  [drsprite](https://wordpress.org/support/users/drsprite/)
 * (@drsprite)
 * [14 years ago](https://wordpress.org/support/topic/on-this-day-php-code-for-template/)
 * 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)

 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [14 years ago](https://wordpress.org/support/topic/on-this-day-php-code-for-template/#post-2774789)
 * [http://codex.wordpress.org/Class_Reference/WP_Query#Time_Parameters](http://codex.wordpress.org/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>';
       } ?>
       ```
   
 * [http://codex.wordpress.org/Function_Reference/get_day_link](http://codex.wordpress.org/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](https://wordpress.org/support/users/drsprite/)
 * (@drsprite)
 * [14 years ago](https://wordpress.org/support/topic/on-this-day-php-code-for-template/#post-2774826)
 * 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.

## Tags

 * [date](https://wordpress.org/support/topic-tag/date/)
 * [function](https://wordpress.org/support/topic-tag/function/)
 * [php](https://wordpress.org/support/topic-tag/php/)
 * [template](https://wordpress.org/support/topic-tag/template/)

 * 2 replies
 * 2 participants
 * Last reply from: [drsprite](https://wordpress.org/support/users/drsprite/)
 * Last activity: [14 years ago](https://wordpress.org/support/topic/on-this-day-php-code-for-template/#post-2774826)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
