Darren Bachan
Darren Bachan

Reputation: 743

WordPress - How can I target the most recent post and modify it?

I'm working on a shortcode and I'd like to target the most recent "Event". Targeting this most recent "Event" I'd like to do two things.

  1. Being able to apply a specific class to it so I can style it differently (I don't want to use first-child).
  2. Add the_excerpt() to it.

Currently the shortcode pulls the most recent 4 "Events". So the most recent would need what I mentioned above. I'd imagine it would have to do something with "count" but I'm not entirely sure, still trying to learn this stuff.

add_shortcode( 'show_events', 'events_query' );

function events_query() {
    $args = array(
        'posts_per_page' => 4,
        'category_name' => 'events',
    );
    $events_query = new WP_Query( $args );
    if ( $events_query->have_posts() ) :
        $html_out = '<article>';
        while ( $events_query->have_posts() ) :
            $events_query->the_post();
            // Do stuff with each post here
            $html_out .= '<div class="events-item"><div class="meta-date">' . Date('F j, Y') . '</div><div class="meta-info"><div class="meta-title"><h4><a href="' . get_permalink() . '">' . get_the_title() . '</a></h4></div>/div></div>';
        endwhile;
        $html_out .= '</article>';
    else : // No results
        $html_out = 'No Events Found.';
    endif;
    wp_reset_query();
    return $html_out;
}

EDIT

Updated using code from an answer:

add_shortcode( 'show_events', 'events_query' );

function events_query() {
    $args = array(
        'posts_per_page' => 4,
        'category_name' => 'events',
    );
    $events_query = new WP_Query( $args );
    if ( $events_query->have_posts() ) :
        $html_out = '<article>';
        $counter = 0;
        $event_class = 'events-item';
        while ( $events_query->have_posts() ) :
            if ( $counter == 0 ) {
                 $event_class = 'events-item most-recent';
            }
            $events_query->the_post();
            // Do stuff with each post here
            $html_out .= '<div class="' . $event_class . '"><div class="meta-date">' . Date('F j, Y') . '</div><div class="meta-info"><div class="meta-title"><h5><a href="' . get_permalink() . '">' . get_the_title() . '</a></h5></div></div>';

            if ( $counter == 0 ) {
                $html_out .= '<div class="meta-info">' . get_the_excerpt() . '</div>';
            }
            $html_out .= '</div>';
            $counter++;
        endwhile;
        $html_out .= '</article>';
    else : // No results
        $html_out = 'No Events Found.';
    endif;
    wp_reset_query();
    return $html_out;
}

Upvotes: 0

Views: 84

Answers (2)

Stanimir Stoyanov
Stanimir Stoyanov

Reputation: 1926

Hey Darren it's me again :) You can use additional var $counter to check if it's the first post Here is the code

add_shortcode( 'show_events', 'events_query' );

function events_query() {
    $args = array(
        'posts_per_page' => 4,
        'category_name' => 'events',
    );
    $events_query = new WP_Query( $args );
    if ( $events_query->have_posts() ) :
        $html_out = '<article>';
        $counter = 0;

        while ( $events_query->have_posts() ) :
            $event_class = 'events-item';
            if ( $counter == 0 ) {
                 $event_class = 'events-item first-event, additiona-classes';
            }
            $events_query->the_post();
            // Do stuff with each post here
            $html_out .= '<div class="' . $event_class . '"><div class="meta-date">' . Date('F j, Y') . '</div><div class="meta-info"><div class="meta-title"><h4><a href="' . get_permalink() . '">' . get_the_title() . '</a></h4></div>/div>';

            if ( $counter == 0 ) {
                $html_out .= '<div class="meta-info">' . $post->post_excerpt . '</div>';
            }
            $html_out .= '</div>';
            $counter++;
        endwhile;
        $html_out .= '</article>';
    else : // No results
        $html_out = 'No Events Found.';
    endif;
    wp_reset_query();
    return $html_out;
}

Upvotes: 1

Piyush Dhanotiya
Piyush Dhanotiya

Reputation: 9

Hello Dear please use the if codition

add_shortcode( 'show_events', 'events_query' );

function events_query() {
    $args = array(
        'posts_per_page' => 4,
        'category_name' => 'events',
    );
    $events_query = new WP_Query( $args );
    if ( $events_query->have_posts() ) :
        $html_out = '<article>';
        $i=1;
        while ( $events_query->have_posts() ) :
            $events_query->the_post();
            if($i==1){
             $class = "new_class";
             $excerpt = the_excerpt(); 
             }
            $html_out .= '<div class="events-item'.$class."><div class="meta-date">' . Date('F j, Y') . '</div><div class="meta-info"><div class="meta-title"><h4><a href="' . get_permalink() . '">' . get_the_title() . '</a></h4>
            <p>'.$excerpt.'</p></div></div></div>';
            $i++;
        endwhile;
        $html_out .= '</article>';
    else : // No results
        $html_out = 'No Events Found.';
    endif;
    wp_reset_query();
    return $html_out;
}

Upvotes: 0

Related Questions