繁体   English   中英

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

[英]Using Wordpress LOOP with pages instead of posts?

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

我希望能够查询一组子页面,然后使用THE 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_parent ,更重要的post_type没有列出。 我挖掘了./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过滤器,但是这只适用于这种情况,如果你需要修改主循环。 当用作辅助循环时,上述示例更好。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM