2013-07-15 67 views
0

我目前正在一個項目的頁面及其層次模仿類別,每個「類別頁面」上的查詢工作正常,但在頂層我希望查詢所有的孫子頁面,並跳過孩子頁面。
In another question on here問他們指出,看起來Search for Children and GrandchildrenWordPress的查詢孫子們只有

但是,代碼不產生任何對我同樣的問題,查詢不返回任何錯誤,只是返回空,一起來看看。

<div id="children"> 
<dl> 
<?php query_posts('static=true&posts_per_page=-1&child_of='.$id.'&order=ASC'); ?> 
<?php if(have_posts()) : while (have_posts()) : the_post(); ?> 
<?php  $inner_query = new WP_Query("post_type=page&posts_per_page=-1&child_of={$id}&order=ASC"); 
while ($inner_query->have_posts()) : $inner_query->the_post(); ?> 
<dt><a href="<?php the_permalink();?>"><?php the_title();?>:</a></dt> 
<dd style=""><em><?php the_excerpt(); ?></em></dd> 
<?php endwhile; endwhile; endif; ?> 
</dl> 
</div> 

我猜$id已取代the_ID();但我看不出這是爲什麼不返回任何結果。

任何想法這裏有什麼問題嗎?

回答

0

I found the answer here。雖然我承認這適用於我的應用程序,但我不確定代碼的確切工作,當然,您使用另一個查詢來詢問查詢以查看是否有任何孫子,但是在細節上有點遺漏implode()是爲了。

<?php 
// set the id of current page 
$gen1_ids = get_the_ID(); 
// query the id for children (or at least that's what I think this query does) 
$gen2 = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE $wpdb->posts.post_parent IN ($gen1_ids) AND $wpdb->posts.post_type = 'page' AND $wpdb->posts.post_status = 'publish' ORDER BY $wpdb->posts.ID ASC"); 
$gen2_ids = implode($gen2,', '); 

//query children if they have children 
$gen3 = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE $wpdb->posts.post_parent IN ($gen2_ids) AND $wpdb->posts.post_type = 'page' AND $wpdb->posts.post_status = 'publish' ORDER BY $wpdb->posts.ID ASC"); 
$gen3_ids = implode($gen3,', '); 
$args=array(
    'post__in' => $gen3, 
    'post_type' => 'page', 
    'post_status' => 'publish', 
    'posts_per_page' => -1, 
    'caller_get_posts'=> 1 
); 
$my_query = null; 
$my_query = new WP_Query($args); 
if($my_query->have_posts()) { 
    while ($my_query->have_posts()) : $my_query->the_post(); 

// Your while code here 

endwhile; 
} //endif 
wp_reset_query(); // Restore global post data stomped by the_post(). 

?> 
+0

'implode($ gen3,',')'沒有意義 - 'post_in'需要一個數組 – djb

相關問題