2012-02-08 72 views

回答

9

你可以使用这个,但它在页面ID的工作而不是标题,如果你真的需要页面的标题,我可以修复它,但ID为更稳定。

<?php 
function get_child_pages_by_parent_title($pageId,$limit = -1) 
{ 
    // needed to use $post 
    global $post; 
    // used to store the result 
    $pages = array(); 

    // What to select 
    $args = array(
     'post_type' => 'page', 
     'post_parent' => $pageId, 
     'posts_per_page' => $limit 
    ); 
    $the_query = new WP_Query($args); 

    while ($the_query->have_posts()) { 
     $the_query->the_post(); 
     $pages[] = $post; 
    } 
    wp_reset_postdata(); 
    return $pages; 
} 
$result = get_child_pages_by_parent_title(12); 
?> 

这一切都记录在这里:
http://codex.wordpress.org/Class_Reference/WP_Query

+0

感谢您使用此快速代码。是的,我同意你的ID更稳定。作为可选的第二个参数,您能否实现返回页面的限制? – 2012-02-08 14:32:57

+0

固定(可以upvote这为tnkx) – janw 2012-02-08 15:11:07

8

我宁愿不WP_Query这样做。虽然它可能不是更有效率,但至少你可以节省一些时间,而不必再一次编写/ have_posts()/ the_post()语句中的所有内容。

function page_children($parent_id, $limit = -1) { 
    return get_posts(array(
     'post_type' => 'page', 
     'post_parent' => $parent_id, 
     'posts_per_page' => $limit 
    )); 
} 
5

为什么不使用get_children()? (一旦它被认为使用ID而不是标题)

$posts = get_children(array(
    'post_parent' => $post->ID, 
    'post_type' => 'page', 
    'post_status' => 'publish', 
)); 

检查the official documentation

相关问题