How to Display Blog Statistics at the Page Footer?


Almost every wordpress theme has a footer.php file that contains the layout for the page footer. You can insert the following PHP+MySQL code to display some statistics of the blog at the end of each page.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
global $wpdb;// https://helloacm.com
$query = "select count(1) from `wp_posts` where `post_status`='publish' and `post_type`='post'";
$cnt_posts = $wpdb->get_var($query);
$query = "select count(1) from `wp_posts` where `post_status`='publish' and `post_type`='page'";
$cnt_pages = $wpdb->get_var($query);
$query = "select count(1) from `wp_comments` where `comment_approved`=1";
$cnt_comments = $wpdb->get_var($query);
$start = strtotime("2011-07-03 00:00:00");  // Need to replace the date to the date when you published your first article.
$today = strtotime(date("Y-m-d h:i:s"));
$days = round(abs($today - $start) / 3600 / 24);
?>
 
Blog is up for <?php echo $days;?> days, and there are <?php echo $cnt_posts;?> posts and <?php echo $cnt_pages;?> pages, there are <?php echo $cnt_comments;?> comments.
<?php
global $wpdb;// https://helloacm.com
$query = "select count(1) from `wp_posts` where `post_status`='publish' and `post_type`='post'";
$cnt_posts = $wpdb->get_var($query);
$query = "select count(1) from `wp_posts` where `post_status`='publish' and `post_type`='page'";
$cnt_pages = $wpdb->get_var($query);
$query = "select count(1) from `wp_comments` where `comment_approved`=1";
$cnt_comments = $wpdb->get_var($query);
$start = strtotime("2011-07-03 00:00:00");  // Need to replace the date to the date when you published your first article.
$today = strtotime(date("Y-m-d h:i:s"));
$days = round(abs($today - $start) / 3600 / 24);
?>

Blog is up for <?php echo $days;?> days, and there are <?php echo $cnt_posts;?> posts and <?php echo $cnt_pages;?> pages, there are <?php echo $cnt_comments;?> comments.

The date when you published your first post is stored at PHP variable $start and therefore, please modify it accordingly. The above PHP code executes three SQL statements for three numbers: the number of posts, the number of pages and the number of comments. The day difference is calculated.

Please also noted that if you have a cache plugin of your wordpress blog, display daily-updated information may force your cache invalidated once per day.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
297 words
Last Post: Require Re-login After User Stays Inactive For More Than 30 Minutes (PHP)
Next Post: Replace WordPress inbuilt Search Box with Google Customize Search

The Permanent URL is: How to Display Blog Statistics at the Page Footer?

Leave a Reply