Skip to main content
1 of 3
Zach Shallbetter
  • 1.2k
  • 7
  • 22
  • 45

Using global $post; with custom post types

I'm working on creating a notification that will indicate when a post is less than a few days old. 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 == "update")) { ?>
    <script type="text/javascript">
        jQuery(document).ready(function($){
            $("#notification").addClass("notification-highlight"); 
        });
    </script>
<?php } ?>
Zach Shallbetter
  • 1.2k
  • 7
  • 22
  • 45