wp query - Loop through array of pages

I am trying to recursively get all children of a page (infinite depth) and I am using function below, from this solution

I am trying to recursively get all children of a page (infinite depth) and I am using function below, from this solution:

function get_all_subpages($page, $args = '', $output = OBJECT) {
    if (! is_numeric($page))
        $page = 0;

    $default_args = array(
        'post_type' => 'page',
    );
    if (empty($args))
        $args = array();
    elseif (! is_array($args))
        if (is_string($args))
            parse_str($args, $args);
        else
            $args = array();
    $args = array_merge($default_args, $args);
    $args['post_parent'] = $page;

    $valid_output = array(OBJECT, ARRAY_A, ARRAY_N);
    if (! in_array($output, $valid_output))
        $output = OBJECT;

    $subpages = array();
    $children = get_children($args, $output);
    foreach ($children as $child) {
        $subpages[] = $child;

        if (OBJECT === $output)
            $page = $child->ID;
        elseif (ARRAY_A === $output)
            $page = $child['ID'];
        else
            $page = $child[0];

        $subpages = array_merge($subpages, get_all_subpages($page, $args, $output));
    }

    return $subpages;
}

What would be the proper way in WordPress to iterate through the returned array so that I can use functions like get_permalink on sub-pages?

I am trying to recursively get all children of a page (infinite depth) and I am using function below, from this solution:

function get_all_subpages($page, $args = '', $output = OBJECT) {
    if (! is_numeric($page))
        $page = 0;

    $default_args = array(
        'post_type' => 'page',
    );
    if (empty($args))
        $args = array();
    elseif (! is_array($args))
        if (is_string($args))
            parse_str($args, $args);
        else
            $args = array();
    $args = array_merge($default_args, $args);
    $args['post_parent'] = $page;

    $valid_output = array(OBJECT, ARRAY_A, ARRAY_N);
    if (! in_array($output, $valid_output))
        $output = OBJECT;

    $subpages = array();
    $children = get_children($args, $output);
    foreach ($children as $child) {
        $subpages[] = $child;

        if (OBJECT === $output)
            $page = $child->ID;
        elseif (ARRAY_A === $output)
            $page = $child['ID'];
        else
            $page = $child[0];

        $subpages = array_merge($subpages, get_all_subpages($page, $args, $output));
    }

    return $subpages;
}

What would be the proper way in WordPress to iterate through the returned array so that I can use functions like get_permalink on sub-pages?

Share Improve this question asked Sep 22, 2019 at 20:56 GeorgePGeorgeP 3571 silver badge10 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

For the above function this would work:

$children = get_all_subpages($cat_id, $args, OBJECT);
foreach ($children as $post) { 
         echo $post->guid;
         echo $post->post_title;
}

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

相关推荐

  • wp query - Loop through array of pages

    I am trying to recursively get all children of a page (infinite depth) and I am using function below, from this solution

    2小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信