WordPress - 我如何定位最新的 post 并修改它?

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

我正在编写简码,我想以最新的 "Event" 为目标。针对最近的 "Event" 我想做两件事。

  1. 能够对其应用特定的 class,这样我就可以设置不同的样式(我不想使用 first-child)。
  2. 加上the_excerpt()

目前短代码提取了最近的 4 "Events"。所以最近的需要我上面提到的。我想它必须用 "count" 做点什么,但我不完全确定,仍在尝试学习这些东西。

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;
}

编辑

使用答案中的代码更新:

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;
}

嗨,达伦,又是我:) 您可以使用额外的 var $counter 来检查它是否是第一个 post 这是代码

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;
}

您好亲爱的,请使用if条件

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;
}