1

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' );
1

Use 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.

  • 1
    Thank you very helpful! I understand now – Jalapeno Jack Mar 30 at 11:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.