Show Me the Numbers: 7 Handy Hacks to Display Your Best Numbers on Your Blog

Show Me the Numbers: 7 Handy Hacks to Display Your Best Numbers on Your Blog

numbersInformation is power, and knowing is half the battle. There are lots of ways to show your good numbers. Maybe you want to highlight a successful blog or author to get others inspired and motivate more people to join in. Bandwagoning has been a classic advertising technique for hundreds of years. Your good numbers will speak for themselves. You want to be able to promote and leverage your best material in a way that will engender a sense of community. Use these hacks to highlight where people are interacting with this website. Give your users the impression that what’s happening on your site is important, and they won’t want to be the only ones missing this information. We’ve put together a list of seven simple hacks to have you showing your good numbers in no time. If you use all of these, it will definitely be overload, so use sparingly and select the best hacks that will enhance your site.

Display Registered Users’ Comment Count on Your WP blog:

How to use: Simply paste this code inside your template files wherever you want to use it.

<?php
global $wpdb;
$where = 'WHERE comment_approved = 1 AND user_id <> 0';
$comment_counts = (array) $wpdb->get_results("
		SELECT user_id, COUNT( * ) AS total
		FROM {$wpdb->comments}
		{$where}
		GROUP BY user_id
	", object);
foreach ( $comment_counts as $count ) {
  $user = get_userdata($count->user_id);
  echo 'User ' . $user->display_name . ' comment count is ' . $count->total . '
';
}
?>

Link to Source

Display Total Number of Twitter Followers:

How to Use: Paste the first section of code in your theme’s functions.php files and then paste the second one anywhere in your template files. Make sure to replace my username with yours.

function string_getInsertedString($long_string,$short_string,$is_html=false){
  if($short_string>=strlen($long_string))return false;
  $insertion_length=strlen($long_string)-strlen($short_string);

  for($i=0;$i<strlen($short_string);++$i){
    if($long_string[$i]!=$short_string[$i])break;
  }
  $inserted_string=substr($long_string,$i,$insertion_length);
  if($is_html && $inserted_string[$insertion_length-1]=='<'){

    $inserted_string='<'.substr($inserted_string,0,$insertion_length-1);
  }
  return $inserted_string;
}

function DOMElement_getOuterHTML($document,$element){
  $html=$document->saveHTML();

  $element->parentNode->removeChild($element);
  $html2=$document->saveHTML();
  return string_getInsertedString($html,$html2,true);
}

function getFollowers($username){
  $x = file_get_contents("http://twitter.com/".$username);

  $doc = new DomDocument;
  @$doc->loadHTML($x);
  $ele = $doc->getElementById('follower_count');
  $innerHTML=preg_replace('/^<[^>]*>(.*)<[^>]*>$/',"\\1",DOMElement_getOuterHTML($doc,$ele));

  return $innerHTML;
}

<?php echo getFollowers("pollyplummer")." followers"; ?>

Link to Source

Display specific author’s posts

How to Use: Get the IDs of the authors you’d like to list the posts. In this example, I want to list posts from authors 1 and 6.

Simply type the following in your browser address bar:

http://yourwpblog.com/?author=6,1

You can add more authors, simply separate all IDs with a comma.

Link to Source

Display Number of Search Results:

How to Use: Open your search.php file and look for the following:

<h2 class="pagetitle">Search Results</h2>

Replace it with this:

<h2 class="pagetitle">Search Results for <?php /* Search Count */ $allsearch = &new WP_Query("s=$s&showposts=-1"); $key = wp_specialchars($s, 1); $count = $allsearch->post_count; _e(''); _e('<span class="search-terms">'); echo $key; _e('</span>'); _e(' — '); echo $count . ' '; _e('articles'); wp_reset_query(); ?></h2>

Link to Source

Display Average Comments Per Post:

How to Use: Paste this code anywhere in your template files where you want the average comments per post to show:

<?php
$count_posts = wp_count_posts();
$posts = $count_posts->publish;

$count_comments = get_comment_count();
$comments  = $count_comments['approved'];

echo "Average ".round($comments/$posts)." comments per post.";
?>

Link to Source

Display Feedburner Count:

How to Use: Paste this piece of code anywhere in your template files where you want to show your Feedburner numbers.

<?php
$fburl=”https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=YourURL“;
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $fburl);
$stored = curl_exec($ch);
curl_close($ch);
$grid = new SimpleXMLElement($stored);
$rsscount = $grid->feed->entry['circulation'];
echo $rsscount;
?>

Link to Source

Display Total Number of Trackbacks:

How to Use: First create a function by adding this to your functions.php file:

function tb_count() {
    global $wpdb;
	$count = "SELECT COUNT(*) FROM $wpdb->comments WHERE comment_type = 'pingback' OR comment_type = 'trackback'";
    echo $wpdb->get_var($count);
}

Then call the function anywhere you want in your template files:

<?php tb_count(); ?>

Link to Source