0

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 } ?>
4
  • 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. – Dave Romsey Nov 8 '11 at 17:30
1

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' );
2
  • 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! – Dave Romsey Jan 17 '12 at 3:45
0

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.

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.