Below I have a function that prints the child pages of a parent. I am trying to obtain the specified parent pages title as a h2 above the unordered list.
I have tried updating the title_li and also the final returned result but cannot work out what what I should be putting in there. What function can I use in one of these places to get the desired result? Thanks!
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 =
'<h2>' . 'Parent title here' . '</h2>' .
'<ul>' . $childpages . '</ul>';
}
return $result;
}
add_shortcode( 'childpages', 'childpages_shortcode_callback' );
Below I have a function that prints the child pages of a parent. I am trying to obtain the specified parent pages title as a h2 above the unordered list.
I have tried updating the title_li and also the final returned result but cannot work out what what I should be putting in there. What function can I use in one of these places to get the desired result? Thanks!
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 =
'<h2>' . 'Parent title here' . '</h2>' .
'<ul>' . $childpages . '</ul>';
}
return $result;
}
add_shortcode( 'childpages', 'childpages_shortcode_callback' );
Share
Improve this question
asked Mar 30, 2019 at 9:52
Jalapeno JackJalapeno Jack
1697 bronze badges
1 Answer
Reset to default 1Use get_the_title
Replace the last part of code
if ( $childpages ) {
$result =
'<h2>' . 'Parent title here' . '</h2>' .
'<ul>' . $childpages . '</ul>';
}
with
if ( $childpages ) {
$result =
'<h2>' . get_the_title( $parent_id ) . '</h2>' .
'<ul>' . $childpages . '</ul>';
}
This may also help.