Did you know: this website doesn't support this OLD Browser. Please Update it or

You should try out most popular browsers in the world Firefox or Google Chrome.

Useful WordPress Code snippets

You are here: Home » Tutorials » Wordpress » Useful WordPress Code snippets
2       comments {by: } Posted in: Wordpress

Below are some of the useful code snippets for wordpress, I am also using some of these wordpress code snippets in my blog. These are also useful in showing stats of your WordPress blog.

1 > Showing Total WordPress Blog Comments

You can paste the below code anywhere in your post or in the footer.php of your blog

<?php $numcomms = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_approved = '1'");
if (0 < $numcomms) $numcomms = number_format($numcomms); ?>
<?php echo "".$numcomms." total blog comments"; ?>

Explanation

First three lines in php code is for getting the approved comments into a variable called $numcomms and the second php code with echo statement is showing the value anywhere you will paste it. Now the second php code with echo is just for controlling it and using it anywhere in the page you like. You can paste it within html code like lists or paragraphs.

2 > Showing Total Registered Blog Users

The code below will show the number of registered users with your blog

<?php $users = $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->users"); ?>
<?php echo $users." registered users."; ?>

Explanation

In the first line we are getting the values in variable $users and in the next line we are displaying it with echo statement and with dot operator we are attaching it with some text. You can change the text to any you like where it says registered users

3 > Showing Popular posts with thumbnail & details

This code will get most commented 5 posts to display them inside a list

<h1>Popular Posts</h1>
<ul id="popular-comments">
<?php $my_q = new WP_Query('orderby=comment_count&posts_per_page=5'); ?>
<?php while ($pc->have_posts()) : $my_q->the_post(); ?>
<li>
<span class="populartitle">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail(array(64,64)); ?></a>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
</span>
<p>Posted on
    <?php the_time('F jS, Y') ?> with
    <?php comments_popup_link('No Comments', '1 Comment', '% Comments'); ?></p>
</li>
<?php endwhile; ?>
</ul>

Explanation

We are looping the 5 posts with most comments and showing their titles and their post thumbnail of 64px size. Also we are showing date posted in paragraph with No. of comments

4 > Using Custom post image or thumbnail sizes

You can paste this code in your wordpress theme functions.php file and can use custom sizes of post images shown on your main blog page

//for functions.php file
if ( function_exists( 'add_theme_support' ) ) {

	add_theme_support( 'post-thumbnails' ); // enable feature

	add_image_size( 'new-post-image', 550, 160, true ); // custom size

	set_post_thumbnail_size(150, 150, true);
	}

The code below is for index.php file and insert it into the loop after this line

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="post">
<div class="thumb"> //settings for post thumnail
<?php the_post_thumbnail( 'new-post-image' ); ?> // insert "custom size" image
</div>
</div>

5 > Linking Post Thumbnail to the post

You can also use the_permalink() to link your post thumbnail to your original post. Remember this code is normally used within a loop

<div class="thumb">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'new-post-image' ); ?>
</a>
</div>

Notes

Also in the next article i am going to tell about using Count Per day Plugin and showing blog stats with it. Support me by sharing it with others, thanks you!


 

Page 1 of 11

2 Responses to “Useful WordPress Code snippets”

  1. 1
    Alexander says:

    Hi, Neat post. There’s a problem with your website in internet explorer, would test this… IE still is the market leader and a large portion of people will miss your excellent writing due to this problem.

    • 1.1
      Ahsan Idrisi says:

      I have just tested it now, and its almost working fine in IE8, but in IE7 it have a lot of issues……………I will plan later to fix those issues…You are right still almost 30% people use Internet Explorer

Leave a Reply