开发者

How to get the number of sub-page/child pages that exist in Wordpress?

开发者 https://www.devze.com 2023-01-10 11:26 出处:网络
Basically I am trying to show something like \"Viewing PageNumber of TotalNumberOfPages\" in each subpage for a specific parent page. So i am trying to figure out how to count the number of s开发者_St

Basically I am trying to show something like "Viewing PageNumber of TotalNumberOfPages" in each subpage for a specific parent page. So i am trying to figure out how to count the number of s开发者_StackOverflow社区ub-pages/child pages that exist and display them on a subpage?


To build on Calle's answer.

//get the sub pages for the parent page
    $subPages = get_pages('child_of'=>parent_page_id);
    //$i will equal the page number
    $i=1;
    foreach($subPages as $subs){
        //not sure what the exact array key for page_id will be. use var_dump($subPages)to find out
        //make the array key the page id for use later (as current_page_id)
        $pageNumber[$subs->page_id] = $i;
    }
    $numberPages = count($pageNumbers);

Then show the page count and number pages for the page you are on.

echo 'vewing page '.$pageNumber[current_page_id].' of '.$numberPages;


You can use count(get_pages('child_of' => page_id)) to get the number of sub-pages. But in order to display that it's for example page 2/5 you have to define somewhere which sub-page is which number. I'd suggest you do that in a custom field. Just put 1, 2, 3, 4, 5, respectively in a custom field in each page that you call number or something.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号