• Resolved kelvinkong

    (@kelvinkong)


    Hi all! I really love this plugin. However, I am really bad at PHP and I can’t seem to find the answer to this. Basically, this is the code that I’m applying:

    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">Last updated on '. $updated_date . ' at '. $updated_time .'</p>';  
    } 
     
        $custom_content .= $content;
        return $custom_content;
    }
    add_filter( 'the_content', 'wpb_last_updated_date' );

    How should I adjust the code so that it only applies to posts and not pages. Thanks!

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Plugin Author Shea Bunge

    (@bungeshea)

    You can add an is_page() check to the beginning of the function to short-circuit on pages:

    add_filter( 'the_content', function ( $content ) {
    	
    	if ( is_page() ) {
    		return $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">Last updated on '. $updated_date . ' at '. $updated_time .'</p>';  
    	} 
     
    	$custom_content .= $content;
    	return $custom_content;
    } );
Viewing 1 replies (of 1 total)
  • The topic ‘Applying PHP code to only Posts and not Pages’ is closed to new replies.