0

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"

  • 1
    I guess calculating if there is a new post in category on demand would be an intensive task. I suggest you store the time of the last post in each category as an option upon post submit. This way you can have what you need in a matter of one function call. – Hamed Momeni Apr 7 '13 at 18:22
  • Please post the code that generates the menus. – s_ha_dum Apr 7 '13 at 18:27
  • The menu code is basically links to pages setup in wordpress, it's nothing special. For example: <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 Apr 8 '13 at 17:21
0

In 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!";
?>
0

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.

0

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!' : '';

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.