0

I am using wp_list_pages to display our website's navigation.

The nav is supposed to be 2nd level pages only. I want the nav to display the array's first four items and then nest the rest of the items in a "more" dropdown.

What I'm trying to do is split the array so that the last few list items can be located in a different HTML tag that contains the more dropdown.

To display the first four items, I use 'include', however that requires the page ID. What I want is the first four items of the array no matter what the page ID is.

Two quick questions: How do I retrieve those first four items in the array? Is this the best way to split the array so that I can display the first four items and have the rest in a More dropdown?

Thanks in advance!

<?php
               wp_list_pages(
                     array(
                       'child_of' => $section_top_parent,
                       'post_type' => 'section',
                       'title_li' => NULL,
                       'depth' => 1,
                       'include' => array(2285, 34272, 2286, 2287),
                       'sort_order' => 'asc'
                   ));
 ?>

1 Answer 1

0

to get pages as array use get_pages() instead. now once we have a list we can check the size of list and split it. Let's have an example:

$allpages = get_pages(
    array(
        'child_of' => $section_top_parent,
        'post_type' => 'section',
        'depth' => 1,
        'sort_order' => 'asc'
    )
);
//check $allpages has any value
if($allpages){
    //calculate the array size
    $totalSize = sizeof($allpages);

    //now divide the array
    $firstFour = array_slice($allpages, 0, 4);
    
    $rest = array();
    //Check if the array has more than 4 items.
    if($totalSize > 4){

        //As we already got first for so we only need the rest of the pages.
        $rest = array_slice($allpages, 4, $totalSize-4 );
    }

    //now you can loop through both $firstFour & $rest to show them as you like.
}

You can a do foreach loop to present the items in your custom htlm.

3
  • Thank you for your answer. get_pages() gives me all the site's pages and negates the "depth" parameter in wp_list_pages() unfortunately, and it strips the links. I tried using "number" and "offset" parameters in the array, but those don't seem to work either :( 'child_of' => $section_top_parent, 'post_type' => 'section', 'title_li' => NULL, 'depth' => 1, 'number' => 2, 'offset' => 2, 'sort_order' => 'asc', Commented Jun 18, 2020 at 18:26
  • Clear something. What $section_top_parent indicates? Commented Jun 19, 2020 at 13:55
  • that variable is the top parent of the custom post type. The array is supposed to display only 2nd-level child pages of the top parent page. Commented Jun 19, 2020 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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