Post numbering function for WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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