functions - List child pages of specific page using shortcode

I have written the function below to list the current pages child pages. Now what I am trying to do is list the children

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
Add a comment  | 

1 Answer 1

Reset to default 2

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745644657a4637897.html

相关推荐

  • functions - List child pages of specific page using shortcode

    I have written the function below to list the current pages child pages. Now what I am trying to do is list the children

    22天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信