admin管理员组文章数量:1333693
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'
));
?>
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'
));
?>
Share
Improve this question
asked Jun 17, 2020 at 18:14
bfoley_teamugbfoley_teamug
111 bronze badge
1 Answer
Reset to default 0to 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.
本文标签: Retrieve array items without page ID
版权声明:本文标题:Retrieve array items without page ID 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742339536a2456315.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论