TonyDiMasi
TonyDiMasi

Reputation: 43

Latest post class in Wordpress page

Is there any way to limit css changes to the latest post page?

For example, I often use the page-id class to limit changes to that particular page.

.page-id-166 p { font-size: 26px; }

I searched the body and found nothing. Is there any class like .page-latest-post?

Upvotes: 1

Views: 93

Answers (1)

Cornel Raiu
Cornel Raiu

Reputation: 3005

You may add the following code to your functions.php file. This will add the 'latest-post' class to the body tag classes.

Docs on wp_get_recent_posts()

Docs on the body_class filter

add_filter( 'body_class', 'mysite_body_classes' );
function mysite_body_classes( $classes ) {
    global $post;

    // change 'post' to whatever your post type is
    if ( is_singular( 'post' ) ) { 
        $args = array(
            'numberposts' => 1,
            'post_status' => 'publish' // only look for published posts
        );

        $recent_posts = wp_get_recent_posts( $args );

        if ( ! empty( $recent_posts ) && $recent_posts[0]['ID'] === $post->ID ) {
            // change your class name to whatever you need it
            $classes[] = 'latest-post'; 
        }
    }
    
    return $classes;
}

Upvotes: 2

Related Questions