开发者

Displaying WordPress page number/count in relation to siblings

开发者 https://www.devze.com 2023-03-04 05:09 出处:网络
I want to add a page number to a part on my page. Something like 7 of 22. I\'ve found a ton of ways to do something similar to this, but not quite this...

I want to add a page number to a part on my page. Something like 7 of 22. I've found a ton of ways to do something similar to this, but not quite this...

First, this is for Pages, not Posts. Secondly, this will be in the si开发者_运维问答debar. The second number must be the total number of sibling pages and the first number should be the current page's position from menu_order.

What's the easiest way to do this?

Thanks


Here's an easy way that will work in your sidebar:

<?php
        $page_id = $post->ID;
        $page_parent = $post->post_parent;
        if ($page_parent) { // there's a parent page, get the children
            $args = array(
                'parent' => $page_parent,
                'child_of' => $page_parent,
                'hierarchical' => 0,
                'sort_column' => 'menu_order'
            );
        }
        else { // no parent, so just get top level pages
            $args = array(
                'parent' => 0,
                'sort_column' => 'menu_order'
            );
        }
        $my_pages = get_pages($args);
        while ($page = current($my_pages)) { // find this page's position in the page array
            if ($page->ID == $page_id) {
                echo key($my_pages) + 1;
            }
            next($my_pages);
        }
        $result = count($my_pages);
        echo " of " . $result;
    ?>
0

精彩评论

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