使用Wordpress LOOP与页面而不是帖子?
有没有办法在Wordpress中使用THE LOOP来加载页面而不是帖子?
我希望能够查询一组子页面,然后使用LOOP函数调用它 - 像和.the_permalink()
the_title()
有没有办法做到这一点?我在文档中没有看到任何内容。query_posts()
有没有办法在Wordpress中使用THE LOOP来加载页面而不是帖子?
我希望能够查询一组子页面,然后使用LOOP函数调用它 - 像和.the_permalink()
the_title()
有没有办法做到这一点?我在文档中没有看到任何内容。query_posts()
是的,这是可能的。您可以创建新的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
补充:还有很多其他参数可以与query_posts一起使用。这里列出了一些,但不幸的是不是全部:http://codex.wordpress.org/Template_Tags/query_posts。至少和更重要post_type
没有列在那里。我挖掘了这些来源。post_parent
./wp-include/query.php
鉴于这个问题的年龄,我想为任何偶然发现它的人提供一个更新的答案。
我建议避免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();
另一种方法是使用pre_get_posts筛选器,但这仅适用于这种情况,前提是您需要修改主循环。当用作辅助循环时,上面的示例更好。