使用Wordpress LOOP页面而不是帖子?

ash*_*her 32 php wordpress

有没有办法在Wordpress中使用THE LOOP来加载页面而不是帖子?

我希望能够查询一组子页面,然后使用THE LOOP函数调用它 - 比如the_permalink()the_title().

有没有办法做到这一点?我在query_posts()文档中没有看到任何内容.

Sim*_*ann 56

是的,这是可能的.您可以创建一个新的WP_Query对象.做这样的事情:

query_posts(array('showposts' => <number_of_pages_to_show>, 'post_parent' => <ID of the parent page>, 'post_type' => 'page'));

while (have_posts()) { the_post();
    /* Do whatever you want to do for every page... */
}

wp_reset_query();  // Restore global post data
Run Code Online (Sandbox Code Playgroud)

增加:有很多其他参数可以与query_posts一起使用.这里列出了一些,但遗憾的是并非全部:http://codex.wordpress.org/Template_Tags/query_posts.至少post_parent更重要的post_type是没有在那里列出.我挖掘了这些来源,./wp-include/query.php以了解这些.


Nat*_*son 20

考虑到这个问题的年龄,我想为偶然发现它的人提供最新的答案.

我建议避免使用query_posts.这是我更喜欢的替代方案:

$child_pages = new WP_Query( array(
    'post_type'      => 'page', // set the post type to page
    'posts_per_page' => 10, // number of posts (pages) to show
    'post_parent'    => <ID of the parent page>, // enter the post ID of the parent page
    'no_found_rows'  => true, // no pagination necessary so improve efficiency of loop
) );

if ( $child_pages->have_posts() ) : while ( $child_pages->have_posts() ) : $child_pages->the_post();
    // Do whatever you want to do for every page. the_title(), the_permalink(), etc...
endwhile; endif;  

wp_reset_postdata();
Run Code Online (Sandbox Code Playgroud)

另一种方法是使用pre_get_posts过滤器,但是这只适用于这种情况,如果你需要修改主循环.当用作辅助循环时,上述示例更好.

进一步阅读:http://codex.wordpress.org/Class_Reference/WP_Query