Wordpress查询仅限孙子女



我目前正在进行一个项目,其中页面及其层次结构模仿类别,每个"类别页面"上的查询都很好,但在顶层,我希望查询所有子页面,并跳过子页面
在这里的另一个问题中,提出了同样的问题,他们被指为寻找儿童和孙子

然而,代码对我来说没有任何结果,查询没有返回任何错误,只是返回空,看看吧。

<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();取代了,但我不明白为什么它没有返回任何结果。

你知道这里出了什么问题吗?

我在这里找到了答案。虽然我承认这对我的应用程序有效,但我不确定代码到底在做什么,当然,你正在用另一个查询查询一个查询,看看是否有孙子,但有点不知道内爆()的作用。

<?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().
?>

最新更新