admin管理员组

文章数量:1425717

In wordpress query, Can I single out the specific post by index number of the query result?

for example

<?php 
            $args =  array( 
                'post_type' => 'headimages',
                'orderby' => 'menu_order',
                'order' => 'ASC'
            );
             $package_query = new WP_Query( $args );
             $num = $package_query->post_count; ?>

so how do i display the second result or third result from this query? Can I do something like this?

<php $thirdresult = $package_query[2];>

please help.

In wordpress query, Can I single out the specific post by index number of the query result?

for example

<?php 
            $args =  array( 
                'post_type' => 'headimages',
                'orderby' => 'menu_order',
                'order' => 'ASC'
            );
             $package_query = new WP_Query( $args );
             $num = $package_query->post_count; ?>

so how do i display the second result or third result from this query? Can I do something like this?

<php $thirdresult = $package_query[2];>

please help.

Share Improve this question asked May 29, 2019 at 7:17 EnamEnam 133 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

The results of a query are stored as an array in the $posts property of the query object, so you can access the third result like so:

$package_query = new WP_Query( $args )
$third_result  = $package_query->posts[2];

本文标签: Access Wordpress query by index number outside the loop