2012-08-10 54 views
1

我试图在循环运行后捕获所有在wp_query中可用的id并将它们放入数组中。我将使用这些ID作为以后的功能。WordPress的:从wp_query构建ID数组

我试过了,但有没有更好的方法?

 $temp = array(); 
    while ($wp_query->have_posts()) : $wp_query->the_post(); 
     $temp[] = $wp_query->post->ID ; 
    endwhile; 
    print_r($temp); 

和得到这样的:

阵列([0] => 7050 [1] => 8227 [2] => 8206 [3] => 8202 [4] => 8200 [5] => 8190 [8] => 8180 [7] => 8174 [8] => 8172 [9] => 8168 [10] => 8162 [11] => 8150 [12] => 8144 = 8138 [14] => 8132 [15] => 8134 [16] => 8130 [17] => 8126 [18] => 8128 [19] => 8124)

回答

7

使用

get_the_ID() 

插入

the_ID() ; 

试试这个

$temp = array(); 
while ($wp_query->have_posts()) : $wp_query->the_post(); 
    $temp[] = get_the_ID() ; 
endwhile; 

print_r($temp);