How to style first post differently with ‘get_template_part’?

If you are using post_class() in your content template like this …

<div <?php post_class(); ?>>

… you can filter that and add a special class:

add_filter( 'post_class', 'mark_first_post' );

function mark_first_post( $classes )
{
    remove_filter( current_filter(), __FUNCTION__ );
    $classes[] = 'first-post';
    return $classes;
}

Your first post in a loop will now have the class first-post. The nice thing is: this filter will run just one time and deactivate itself then.

You can also use another helper function:

function is_first_post()
{
    static $called = FALSE;

    if ( ! $called )
    {
        $called = TRUE;
        return TRUE;
    }

    return FALSE;
}

You can test for the first post then in your template:

if ( is_first_post() )
{
    // render the first post
}
else
{
    // the other posts
}

error code: 523