This nifty little hack will display a post and comment count and can be easily edited, but basically, all you need to do is paste the following code into your PHP file where you want the sentence displayed:

<?php
$numposts = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish'");
if (0 < $numposts) $numposts = number_format($numposts);
$numcomms = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_approved = '1'");
if (0 < $numcomms) $numcomms = number_format($numcomms);
echo $numposts.' articles have been published on this site,';
echo " sharing a total of ".$numcomms." comments.";
?>

This will display XX articles have been published on this site, sharing a total of XX comments, and will only work correctly with Wordpress - if used with MU, it will only display the number of posts and comments specifically for the blog it is seen from…

If you want to define the variables in one place and then re-use the stats as numbers on their own (so they can be better styled) all you need to do is split the code as follows - by first defining the variables:

<?php
$numposts = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish'");
if (0 < $numposts) $numposts = number_format($numposts);
$numcomms = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_approved = '1'");
if (0 < $numcomms) $numcomms = number_format($numcomms);
?>

Then call the numbers using either of the following:

<?php echo $numposts // Is the number of posts ?>
<?php echo $numcomms // Is the number of comments ?>

 

Read More From The WP Hacks Category

 


    Powered by WP Hashcash