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

array - Why are array_slice() and array_chunk() not working?

programmeradmin0浏览0评论

I'm trying to build a nav out of an array of child pages from a custom post type called "section" with a parameter depth of 1. I want to slice this array so that it only displays a part of the array. When I try array_slice() or array_chunk() and then wp_list_pages() to display the pages, it returns everything from the Pages content type and it doesn't split up the array either. What am I doing wrong?

<?php
                 $arr = array(
                    //$section_top_parent is the top parent of the custom post type "section"
                   'child_of' => $section_top_parent,
                   'post_type' => 'section',
                   'title_li' => NULL,
                   'depth' => 1,
                   'sort_order' => 'asc',
                  );

                  $sliced = array_slice($arr, 3, 5);
                  wp_list_pages($sliced);

                 $chunk = array_chunk($arr, 3);
                 wp_list_pages($chunk);

                  ?>

I'm trying to build a nav out of an array of child pages from a custom post type called "section" with a parameter depth of 1. I want to slice this array so that it only displays a part of the array. When I try array_slice() or array_chunk() and then wp_list_pages() to display the pages, it returns everything from the Pages content type and it doesn't split up the array either. What am I doing wrong?

<?php
                 $arr = array(
                    //$section_top_parent is the top parent of the custom post type "section"
                   'child_of' => $section_top_parent,
                   'post_type' => 'section',
                   'title_li' => NULL,
                   'depth' => 1,
                   'sort_order' => 'asc',
                  );

                  $sliced = array_slice($arr, 3, 5);
                  wp_list_pages($sliced);

                 $chunk = array_chunk($arr, 3);
                 wp_list_pages($chunk);

                  ?>
Share Improve this question asked Jun 25, 2020 at 19:01 bfoley_teamugbfoley_teamug 111 bronze badge 1
  • look carefully what your $arr doing. It's doing nothing. It's just containing the arguments. You have to run the query first. – Sabbir Hasan Commented Jun 25, 2020 at 20:20
Add a comment  | 

1 Answer 1

Reset to default 0

You need to do the slice on the results, not the arguments to the function. So:

$arr = array(
    //$section_top_parent is the top parent of the custom post type "section"
    'child_of' => $section_top_parent,
    'post_type' => 'section',
    'title_li' => NULL,
    'depth' => 1,
    'sort_order' => 'asc',
);

$result = wp_list_pages($arr);
$sliced = array_slice($result, 3, 5);
wp_list_pages($sliced);
发布评论

评论列表(0)

  1. 暂无评论