Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm working on creating a notification that will indicate when a post is less than a few days old. This is specifically for a custom post type called "news". Is there a way to use global $post; with custom post types?

This is my original code.

<?php global $post;
    $postdate = str_replace("-", " ",substr($post->post_date, 0,10));
    $postd = explode(" ", $postdate);
    $ageunix = get_the_time('U');
    $days_old_in_seconds = ((time() - $ageunix));
    $days_old = (($days_old_in_seconds/86400));
    if ($days_old < 3) { ?>
    <script type="text/javascript">
        jQuery(document).ready(function($){
            $("#notification").addClass("notification-highlight"); 
        });
    </script>
<?php } ?>

I tried

<?php 
$post_type = get_post_type();
if (($days_old < 3) && ($post_type == "news")) { ?>
    <script type="text/javascript">
        jQuery(document).ready(function($){
            $("#notification").addClass("notification-highlight"); 
        });
    </script>
<?php } ?>
share|improve this question
    
Where is this script being run? –  v0idless Nov 4 '11 at 20:23
    
Outside of the loop. –  Zach Shallbetter Nov 4 '11 at 20:27
    
I have a feeling this should be inside of the loop. How would I target a specific post type? –  Zach Shallbetter Nov 4 '11 at 21:33
    
@ZachShallbetter I see you've been back to edit your question. Did you try the new_post_alert() callback function hooked to wp_footer that I posted? I did test this and it worked for me. –  goto10 Nov 8 '11 at 17:30

2 Answers 2

How about this? (Add to your theme's functions.php file, or to a plugin.)

function new_post_alert() {
    global $post;
    $ageunix = get_the_time( 'U' );
    $days_old_in_seconds = time() - $ageunix;
    $days_old = $days_old_in_seconds / 86400;
    $post_type = get_post_type();

    if ( ( $days_old < 3 ) && ( 'news' === $post_type ) ) { ?>
        <script type="text/javascript">
            jQuery(document).ready(function($){
                $("#notification").addClass("notification-highlight");
                //alert('new!');
            });
        </script><?php
    }
}
add_action( 'wp_footer', 'new_post_alert' );
share|improve this answer
    
Any reason the third and fourth lines are there? They don't seem to do anything. –  Manny Fleurmond Jan 16 '12 at 1:35
    
Hi Manny. You're right. I went ahead and removed the extra lines. I didn't really scrutinize the date stuff from the original code and just tossed it into a function so that it could be hooked. Still room for improvement here... On the date stuff, maybe: $days_old = (int) ( ( time() - get_the_time( 'U', $post->ID ) ) / 86400 ); Thinking about it even more, it would probably be better to hook into post_class rather than using JS for adding that notification class too. Even then, there's still room for improvement here. Anywhoo, cheers! –  goto10 Jan 17 '12 at 3:45

Which template are you doing this in? You can use global $post with a CPT, as long as you are inside a loop when you call the function.

Specifically, you might consider putting your code in the loop in the single-news.php template.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.