我正在编写一个短代码,我想定位最近的"事件"。针对最近的"事件"我想做两件事。
first-child
)。the_excerpt()
添加到其中。目前,短代码会拉动最近的4"事件"。所以最近需要我上面提到的。我想象它必须用"计数"但我还不完全确定,还在努力学习这些东西。
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;
}
答案 0 :(得分:1)
$counter
来检查它是否是第一篇文章
这是代码
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;
}
答案 1 :(得分:0)
您好亲爱的请使用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;
}