我目前正在开展一个项目,其中页面及其层次结构模仿类别,每个“类别页面”上的查询工作正常,但在顶层我希望查询所有孙子页面,并跳过子页面。
In another question on here询问他们被指出的同一个问题看起来是Search for Children and Grandchildren
但是,代码对我没有任何影响,查询不返回任何错误只返回空,看看。
<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 :(得分: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().
?>