Search code examples
wordpresslast-modified

How To Prevent - Last Modified Date/Time - to not show on Pages but only Posts


I have created a separate plugin for WordPress (that's usually called the site-specific plugin) where I added a function to display the last modified date and time. Well, it works good but I don't want to display the same for PAGES but only for Posts.

What should I modify in this code?

    function wpb_last_updated_date( $content ) {
$u_time = get_the_time('U'); 
$u_modified_time = get_the_modified_time('U'); 
if ($u_modified_time >= $u_time + 86400) { 
$updated_date = get_the_modified_time('F jS, Y');
$updated_time = get_the_modified_time('h:i a'); 
$custom_content .= '<p class="last-updated"><b>Last updated on</b> '. $updated_date . ' at '. $updated_time .'</p>';  
} 

    $custom_content .= $content;
    return $custom_content;
}
add_filter( 'the_content', 'wpb_last_updated_date' );

Solution

  • You can check which page is being displayed with these conditions:

    is_page()     //For pages
    is_single()   //for posts
    is_singular() //for posts AND pages
    is_category() //for categories
    is_tag()      //for tags
    is_404()      //for 404 page
    

    Try putting code below which have condition to add custom content to only the posts:

    function wpb_last_updated_date( $content ) 
    {
          $u_time = get_the_time('U'); 
          $u_modified_time = get_the_modified_time('U');
    
          if ($u_modified_time >= $u_time + 86400) 
          { 
               $updated_date = get_the_modified_time('F jS, Y');
               $updated_time = get_the_modified_time('h:i a'); 
               if(is_single()) 
               {
                     $custom_content .= '<p class="last-updated"><b>Last updated on</b> '. $updated_date . ' at '. $updated_time .'</p>';  
               }
          }
          $custom_content .= $content;
          return $custom_content;
    }
    add_filter( 'the_content', 'wpb_last_updated_date' );
    

    For a more complete list of template tags check visit: http://codex.wordpress.org/Function_Reference/is_page