wordpress:如何在一页上显示多个页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1309382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
wordpress: How can I display multiple pages on one page?
提问by Joseph Carrington
Let's say I have three different pages, page1, page2, and page3.
假设我有三个不同的页面,page1、page2 和 page3。
I want page1 and page2 to display on my static front page. Do I restrict the loop to only pull page1 and page2, or do I need to start the loop, test for name="page1" or something like that, and then print? Thanks! -Joe
我希望 page1 和 page2 显示在我的静态首页上。我是否将循环限制为仅拉出 page1 和 page2,或者我是否需要开始循环,测试 name="page1" 或类似的东西,然后打印?谢谢!-乔
回答by Miriam Suzanne
I would skip the loop and simply use get_page($id)
as described here:
我会跳过循环并简单地get_page($id)
按照此处的描述使用:
http://codex.wordpress.org/Function_Reference/get_page
http://codex.wordpress.org/Function_Reference/get_page
All you need to know is the ID's of your pages, and you can pull them one at a time anywhere you want.
您只需要知道页面的 ID,您就可以在任何地方一次拉取一个。
回答by John
Here is an example of how you could do it. This code will work if you have all the pages that you want to be displayed under one parent. In this case, I was putting pages under the home page (p.post_parent = 2).
这是一个如何做到这一点的示例。如果您希望在一个父项下显示所有页面,则此代码将起作用。在这种情况下,我将页面放在主页下(p.post_parent = 2)。
if ($post->post_type == 'page') {
$pages = $wpdb->get_results("SELECT p.ID, p.post_name, p.post_title, p.post_parent, pm.meta_value FROM $wpdb->posts AS p LEFT JOIN $wpdb->postmeta AS pm ON pm.post_id=p.ID AND pm.meta_key='wp_menu_nav' LEFT JOIN $wpdb->posts AS P ON P.ID=p.post_parent WHERE p.post_parent = 2 AND p.post_type='page' AND p.post_status='publish' ORDER BY p.menu_order ASC");
if ($wpdb->num_rows > 0) {
foreach($pages as $page) {
//echo $page->ID . "<br>";
$args = array( 'numberposts' => 1, 'post_type'=> 'page', 'include' => $page->ID, 'post_status' => 'published' );
$myposts = get_posts($args);
foreach($myposts as $mypost) {
setup_postdata($mypost);
echo the_content();
}
}
}
}