Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Is there a way of putting "Post X of Y" into my sidebar? I think I can only show it when it is a single blog post (something like checking if the content is_single) but I want to be able to see which post I am viewing. Googling "wordpress post x of y" for the last 20 minutes has given me a lot about category x in page y or something similar, so I thought to ask here.

share|improve this question
add comment

1 Answer 1

up vote 0 down vote accepted
class MY_Post_Numbers {

    private $count = 0;
    private $posts = array();

    public function display_count() {
        $this->init(); // prevent unnecessary queries
        $id = get_the_ID();
        echo sprintf( '<div class="post-counter">Post number<span class="num">%s</span><span class="slash">/</span><span class="total">%s</span></div>', $this->posts[$id], $this->count );
    }

    private function init() {
        if ( $this->count )
            return;
        global $wpdb;       
        $posts = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_date " ); // can add or change order if you want 
        $this->count = count($posts);

        foreach ( $posts as $key => $value ) {
            $this->posts[$value] = $key + 1;
        }
        unset($posts);
    }

}
$GLOBALS['my_post_numbers'] = new MY_Post_Numbers;

function my_post_number() {
    $GLOBALS['my_post_numbers']->display_count();
}

to use in template file :

<?php my_post_number(); ?>
share|improve this answer
    
So the function would go into my functions.php file, and the call in single.php or sidebar.php? –  Nathan Bunn Dec 5 '11 at 11:04
    
the function will go to functions.php (actually, it is a class) . the call can be anywhere you want ... Please tell me if it works ok or if you encounter any problems .. –  krembo99 Dec 5 '11 at 12:52
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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