admin管理员组文章数量:1294707
I have 3 different custom post types, with each type forming my main navigation. When I post something new under one of them, I want that category in the nav to be marked with a "new" label for a limited time (7 days for example).
I've found a few decent posts on how to apply it to single posts, but I can't figure out how to apply it to multiple categories in the top navigation.
The code for single posts is below should that be of any use:
function newly_posted() {
global $post;
$now = date('U'); $published = get_the_time('U');
$new = false;
if ( $now-$published <= 3*24*60*60 ) $new = true;
return $new;
}
Any help would be appreciated!
EDIT: As mentioned in the comments, I'm I'm basically looking for something that will do: If post type X has new post within 7 days, add class "new"
I have 3 different custom post types, with each type forming my main navigation. When I post something new under one of them, I want that category in the nav to be marked with a "new" label for a limited time (7 days for example).
I've found a few decent posts on how to apply it to single posts, but I can't figure out how to apply it to multiple categories in the top navigation.
The code for single posts is below should that be of any use:
function newly_posted() {
global $post;
$now = date('U'); $published = get_the_time('U');
$new = false;
if ( $now-$published <= 3*24*60*60 ) $new = true;
return $new;
}
Any help would be appreciated!
EDIT: As mentioned in the comments, I'm I'm basically looking for something that will do: If post type X has new post within 7 days, add class "new"
3 Answers
Reset to default 1In case anyone else is looking for this in the future, turns out the best way to do it is by using wp_get_recent_posts
function. Someone knocked up an example that worked great for me:
<?php $args = array( 'numberposts' => '1' );
$recent_post = wp_get_recent_posts( $args );
if (strtotime($recent_post[0]['post_date']) > strtotime('-7 days'))
$class = "new!";
?>
It's somehow easy is IF you know some PHP and some of WP functions. If you have your own hand-written navigation menu, then it would be easier to implement.
It seems you know PHP, So you can check post types with this function:
function does_posttype_have_new_post($post_type='post', $days = 7){
$posts = get_posts('posts_per_page=1&post_type='.$post_type);
$post_date = date_create($posts[0]->post_date);
return intval(date_diff($post_date,date_create(),true)->format("%R%a")) <= $days;
}
You can use it like does_posttype_have_new_post('book')
to check if book
post type had any new posts in this week. By passing the second parameter, you can change the range of days. Use it as a IF condition like:
if(does_posttype_have_new_post('book')) echo 'New!';
or apply a filter and use this function to check posts in a post type.
EDIT: I have read the question again, it seems you want to apply this to a dynamic navigation menu (added in WP 3.0), which is the very very hard way.
You are going to use Hooks and Filters.
You have to first parse the HTML (Using Regex or something like SimpleHTMLDom), see where the pages link to, then check for posts in post type, add the class and return the filtered content.
It's time consuming but if you want to not touch your theme's code and keep it clean, you can do it this way.
My recommendation is: If you don't change your menu constantly, then use a static menu, and do it the easy way.
In my opinion, a better way to do this is using transients. This way we would not be querying the database for each post type every time page is refreshed.
Simple hook would do it:
add_action( 'wp_insert_post', 'wpse94916_set_new_flag', 10, 3 );
function wpse94916_set_new_flag( $post_id, $post, $update ) {
if ( $update ) {
return;
}
set_transient( 'is_new_' . $post->post_type, true, WEEK_IN_SECONDS );
}
This will set/update a transient on every new post save. And then to use it, just add a simple check:
$post_type = 'post_type';
$class = get_transient( 'is_new_' . $post_type ) ? 'new!' : '';
本文标签: navigationNew posts label category with quotnewquot
版权声明:本文标题:navigation - New posts label category with "new" 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741596718a2387456.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
option
upon post submit. This way you can have what you need in a matter of one function call. – 2hamed Commented Apr 7, 2013 at 18:22<li id="nav-design"><a href="<?php echo get_settings('home'); ?>/design">
</a></li>` In pseudo code, I'm basically looking for something that will do:If post type X has new post within 7 days, add class "new"
– Tom Commented Apr 8, 2013 at 17:21