2012-01-06 32 views
0

我在wordpress中进行自定义查询,只检索页面ID#20的子页面。只查询页面的孩子,在page.php

而且只待与该ID的页面可见#20#95#97

<?php if (is_page(array('20','95','97'))) /* RIDERS */ { query_posts(array(

    'post_type' => 'page', 

    'child_of' => 20, 

    'order' => 'DESC' 

)); } ?> 

<?php if (have_posts()) : while (have_posts()) : the_post(); ?> 

    <!-- my loop stuff here -->   

<?php endwhile; endif; wp_reset_query(); ?> 

我的第一个问题是,这个循环是在我的page.php文件模板文件(主页外循环)。由于某种原因,页面数据正在显示在我上面的查询中。我怎样才能收紧上面的这个查询,所以它不会干扰页面上的其他循环。我想这是因为我的page.php模板上有2 <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

我的下一个问题是,我写了这个查询是否正确?因为它似乎列出了其他页面而不仅仅是页面ID#20的子页面?

很多感谢您的帮助。

乔希

回答

0

怎么像

<?php 
// just show this on your selected pages.. 
if (is_page(array('20','95','97'))) /* RIDERS */ { 
    query_posts(array('post_type' => 
        'page','child_of' => 20, 
        'order' => 'DESC' 
        ) 
      ); 
    if (have_posts()) : while (have_posts()) : the_post(); 
?> 
    <!-- my loop stuff here --> 
<?php 
    endwhile; 
    else: 
?> 
    <h2>No Content</h2> 
<?php 
    endif; 
}else{ 
//else just show normal post 
    if (have_posts()) : while (have_posts()) : the_post(); ?> 
     <!-- my loop stuff here --> 
<?php 
    endwhile; 
    else: 
?> 
    <h2>No Content</h2> 
<?php 
    endif; 
} //end of normal post 
?> 

希望帮助? M ..

只是回头看过帖子,我看到它的页面试图使用query_posts函数进行查询,具体取决于您要在循环中显示的内容,最好使用wp_list_pages()列出子项父母的页面。

+0

感谢烈士 - 这工作。 – Joshc 2012-01-14 14:32:39