Skip to content

Instantly share code, notes, and snippets.

@mattwiebe
Created March 16, 2011 18:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mattwiebe/873001 to your computer and use it in GitHub Desktop.
Save mattwiebe/873001 to your computer and use it in GitHub Desktop.
Post numbering function for WordPress
<?php
/**
* Display a post count on WordPress posts
* Just use mw_post_number() inside the loop and you'll get a post number count
* @author Matt Wiebe
* @link http://somadesign.ca/
* @license GPL v2 {@link} http://www.opensource.org/licenses/gpl-2.0.php
* @copyright 2011
*/
class MW_Post_Numbers {
private $count = 0;
private $posts = array();
public function display_count() {
$this->init(); // doing it this way to prevent unnecessary queries
$id = get_the_ID();
echo sprintf( '<div class="post-counter"><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 " );
$this->count = count($posts);
foreach ( $posts as $key => $value ) {
$this->posts[$value] = $key + 1;
}
unset($posts);
}
}
$GLOBALS['mw_post_numbers'] = new MW_Post_Numbers;
function mw_post_number() {
$GLOBALS['mw_post_numbers']->display_count();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment