最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - Loop through array of pages

programmeradmin0浏览0评论

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;
}
发布评论

评论列表(0)

  1. 暂无评论