0

I need to get a different "get_template_part" based on the date and category. I have changed the outlay of the category which means any post in that category before today's date wont be able to use the new template but needs to still use the old one.

if ( in_category( 'photographer-interviews' )) {
 get_template_part( 'content', 'interview' );
} else {
 get_template_part( 'content', 'blog' );
}

So i need to also check if the post inside the "photographer-interviews" was posted after today. If it was continue to the new template if not keep using the old one (blog).

3
  • And by "today" you mean 15th Aug, 2013? Which template should be used when exactly? Aug 15 '13 at 18:11
  • The interview template needs to be used for anything posted after 15th Aug, 2013 in the photographer-interviews category. Aug 15 '13 at 18:21
  • u have post object or something? if you are in loop you can use if (strtotime(the_date('', '', '', false)) <= strtotime(yesterday)) { get_template_part( 'content', 'interview' ); } else { get_template_part( 'content', 'blog' ); }
    – Kumar
    Aug 15 '13 at 18:34
1

There are many ways of comparing dates. the following is perhaps the simplest (untested):

if ( in_category( 'photographer-interviews' ) && strtotime( get_the_date( 'c' ) ) > 1376524800 ) {
 get_template_part( 'content', 'interview' );
} else {
 get_template_part( 'content', 'blog' );
}

1376524800 is the Unix timestamp for 00:00 (GMT) on August 15 - that is, midnight of the 14th. So posts with a publication date of the 15th will return true.

If you literally only want posts after the 15th you'd use 1376611200.

1

Here are the steps to compare the post date against a set date.

Get the post date

This will retrieve the date in the format yyyy-mm-dd as in 2013-07-31. The Wordpress Codex has more info on the_date().

$post_date = the_date( 'Y-m-d', '', '', false );

Specify cutoff date

Specify the date to compare the post date against. Use the same date format as in step 1.

$cutoff_date = '2013-08-14';

Convert dates

Convert the dates to a timestamp string using PHP's strtotime() function so that they can be compared. Add this to the code from above.

$post_date = strtotime( the_date( 'Y-m-d', '', '', false ) );
$cutoff_date = strtotime( '2013-08-14' );

Compare dates

$post_date > $cutoff_date

All together now

And here's the complete code.

$post_date = strtotime( the_date( 'Y-m-d', '', '', false ) );
$cutoff_date = strtotime( '2013-08-14' );
if ( in_category( 'photographer-interviews' ) && $post_date > $cutoff_date ) {
    get_template_part( 'content', 'interview' );
} else {
    get_template_part( 'content', 'blog' );
}

3
  • 1
    Your code does not take timezones into account - though I don't know if that degree of accuracy is required by the OP.
    – vancoder
    Aug 15 '13 at 20:00
  • @vancoder Good point. I hadn't considered that level of accuracy.
    – epilektric
    Aug 15 '13 at 22:19
  • Good explanation otherwise though.
    – vancoder
    Aug 15 '13 at 22:32
0

Marvelous, I used the idea to switch between bootstrap 3 and 4.

$post_date = strtotime( get_the_date( 'Y-m-d', '', '', false ) );
$cutoff_date = strtotime( '2019-12-31' );
if (is_single() && $post_date > $cutoff_date) {
echo '<bootstrap 4 code>';
} elseif (is_single() && $post_date < $cutoff_date) { echo '<bootstrap 3 code>';}
elseif (!is_single()) {echo '<bootstrap 4 code>';}

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.