如果我查询wordpress数据库以获取最近发布的五篇文章,并且我想对它们中的每一篇进行不同的处理,是否有方法访问各个实例对象?
例如,如果我想对第二个post对象执行某些操作,是否有一种方法可以通过以下方式访问该对象?
$query = new WP_Query( $args );
$query[1] => access to the second post object from the query
最合适的回答,由SO网友:gmazzap 整理而成
$query = new WP_Query( $args );
if ( $query->have_posts( $query->have_posts()) ) : while( $query->have_posts()) :
$query->the_post();
global $post;
switch ( $query->current_post ) {
case 0 :
// first post object is in var $post;
echo \'The first post title is \' . $post->post_title;
break;
case 1 :
// because we are insed the loop we can use all the post template tags
echo \'The second post title is \' . get_the_title();
break;
....
}
endwhile; endif;
wp_reset_postdata();