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