如何通过Wordpress中的父页面标题获取页面的所有子页面? [英] How to get all child pages of a page by the parent page title in Wordpress?
本文介绍了如何通过Wordpress中的父页面标题获取页面的所有子页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
示例:
About
--- technical
--- medical
--- historical
--- geographical
--- political
如何创建这样的函数?
function get_child_pages_by_parent_title($title)
{
// the code goes here
}
并像这样调用它会返回一个充满对象的数组.
and calling it like this which will return me an array full of objects.
$children = get_child_pages_by_parent_title('About');
推荐答案
你可以用这个,它对页面 ID 而不是标题有效,如果你真的需要页面标题,我可以修复它,但 ID 更稳定.
You can use this, it does work on page ID rather then title, if you really need page title, I can fix it, but ID is more stable.
<?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
这篇关于如何通过Wordpress中的父页面标题获取页面的所有子页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文