2014-01-07 127 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 

希望這有助於!

相關問題