2014-01-07 128 views
3

我想显示一个父页面的随机子页面,但我得到的随机帖子,我不希望被列入显示区域。如何在Wordpress中显示父页面的随机子页面?

$my_query = new WP_Query(array ('orderby' => 'rand', 'posts_per_page' => '1', 'pagename=guide')); 

所以,我要的是显示谁一决高下是引导家长页的随机子页面,而是我看到一些随机的帖子这是我想要的东西完全不同。任何帮助,将不胜感激。谢谢:)

+0

http://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters你要使用'post_parent = '而不是页面名称=引导我认为 – EdgeCaseBerg

+0

这并不太多工作:( – user3166421

+0

它会显示与之前显示的结果相同的结果 – user3166421

回答

4

这是为我工作。 post_type是重要的部分,否则它似乎不会WP查询对页面。 post_parent应该是你的页面的整数ID。

$query = new WP_Query(array('orderby' => 'rand', 'posts_per_page' => 1, 'post_type' => 'page', 'post_parent' => '2')) ; 
if ($query->have_posts()) { 
     echo '<ul>'; 
    while ($query->have_posts()) { 
     $query->the_post(); 
     echo '<li>' . get_the_title() . '</li>'; 
    } 
     echo '</ul>'; 
} else { 
    // no posts found 
} 
+0

非常感谢:)也为我工作 – user3166421

0

这为我工作:

$my_wp_query = new WP_Query(); // Setup query object 
$all_wp_pages = $my_wp_query->query(array('post_type' => 'page')); // Get all pages 
$children = get_page_children(get_the_id(), $all_wp_pages); // Get the children pages from list of all pages 
$rand = rand(0, (count($children) - 1)); // Count children and get a random number from that range 
print_r($children[$rand]); // Print random child object 

希望这有助于!

相关问题