最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

templates - If Posted After Date

programmeradmin3浏览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).

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).

Share Improve this question asked Aug 15, 2013 at 17:52 Noob TheoryNoob Theory 15512 bronze badges 3
  • And by "today" you mean 15th Aug, 2013? Which template should be used when exactly? – Krzysiek Dróżdż Commented Aug 15, 2013 at 18:11
  • The interview template needs to be used for anything posted after 15th Aug, 2013 in the photographer-interviews category. – Noob Theory Commented Aug 15, 2013 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 Commented Aug 15, 2013 at 18:34
Add a comment  | 

3 Answers 3

Reset to default 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.

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' );
}

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>';}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论