admin管理员组文章数量:1434978
I have written the function below to list the current pages child pages. Now what I am trying to do is list the children of a specific page using its slug in the shortcode.
Is it possible to sneak an attribute into wp_list_pages? So for example if you wanted to list the pages of a certain parents slug you could specify the parent in the shortcode like this [childpages parent=”projects″] I am kind of stumped here
function list_child_pages()
{
global $post;
$childpages = wp_list_pages('sort_column=menu_order&title_li=&child_of=' . $post->post_parent . '&echo=0');
if ($childpages)
{
$string = '<ul>' . $childpages . '</ul>';
}
return $string;
}
add_shortcode('childpages', 'list_child_pages');
I have written the function below to list the current pages child pages. Now what I am trying to do is list the children of a specific page using its slug in the shortcode.
Is it possible to sneak an attribute into wp_list_pages? So for example if you wanted to list the pages of a certain parents slug you could specify the parent in the shortcode like this [childpages parent=”projects″] I am kind of stumped here
function list_child_pages()
{
global $post;
$childpages = wp_list_pages('sort_column=menu_order&title_li=&child_of=' . $post->post_parent . '&echo=0');
if ($childpages)
{
$string = '<ul>' . $childpages . '</ul>';
}
return $string;
}
add_shortcode('childpages', 'list_child_pages');
Share
Improve this question
asked Mar 30, 2019 at 7:07
Jalapeno JackJalapeno Jack
1697 bronze badges
1 Answer
Reset to default 2wp_list_pages
can take child_of
param, to show only pages that are children of given page. But you have to pass the ID of that parent page (so you can't put a slug in there).
But you can use get_page_by_path
to get a page object based on slug of the page.
And another thing you have to do is to add some params to your shortcode.
So here's the code:
function childpages_shortcode_callback( $atts ) {
$atts = shortcode_atts( array(
'parent' => false,
), $atts, 'childpages' );
$parent_id = false;
if ( $atts['parent'] ) {
$parent = get_page_by_path( $atts['parent'] );
if ( $parent ) {
$parent_id = $parent->ID;
}
} else { // if no parent passed, then show children of current page
$parent_id = get_the_ID();
}
$result = '';
if ( ! $parent_id ) { // don't waste time getting pages, if we couldn't get parent page
return $result;
}
$childpages = wp_list_pages( array(
'sort_column' => 'menu_order',
'title_li' => '',
'child_of' => $parent_id,
'echo' => 0
) );
if ( $childpages ) {
$result = '<ul>' . $childpages . '</ul>';
}
return $result;
}
add_shortcode( 'childpages', 'childpages_shortcode_callback' );
本文标签: functionsList child pages of specific page using shortcode
版权声明:本文标题:functions - List child pages of specific page using shortcode 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745644656a2668062.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论