示例:
About
--- technical
--- medical
--- historical
--- geographical
--- political
Run Code Online (Sandbox Code Playgroud)
如何创建这样的功能?
function get_child_pages_by_parent_title($title)
{
// the code goes here
}
Run Code Online (Sandbox Code Playgroud)
并像这样调用它将返回一个充满对象的数组.
$children = get_child_pages_by_parent_title('About');
Run Code Online (Sandbox Code Playgroud)
jan*_*anw 10
您可以使用它,它可以在页面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);
?>
Run Code Online (Sandbox Code Playgroud)
这些都记录在这里:http:
//codex.wordpress.org/Class_Reference/WP_Query
我更喜欢没有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
));
}
Run Code Online (Sandbox Code Playgroud)
为什么不用get_children()?(一旦被认为使用ID而不是标题)
$posts = get_children(array(
'post_parent' => $post->ID,
'post_type' => 'page',
'post_status' => 'publish',
));
Run Code Online (Sandbox Code Playgroud)
查看官方文档.
| 归档时间: |
|
| 查看次数: |
19131 次 |
| 最近记录: |