Adding conditional content to single post-type template

Firstly you’d be better off creating a template file for your specific post type by creating a file called single-newsletter.php. WordPress will pick it up and use it automatically.

Secondly add this function to your functions.php and call it to work out if it’s the most recent one:

function is_latest_newsletter( $post_id = 0 ) {
    $latest = array_shift( get_posts( array(
        'post_type' => 'newsletter',
        'posts_per_page' => 1
    ) ) );

    if ( ! $post_id && in_the_loop() )
        $post_id = get_the_ID();

    return $latest->ID == $post_id;
}

Then in your template file:

if ( have_posts() ) : while ( have_posts() ) : the_post();

    if ( is_latest_newsletter() ) {
        // do stuff
    } 

endwhile; endif;

You could also pass in a specific post ID to the function so it’s usable in other contexts too.

tech