How to Display Most Recent Twitter Tweets in WordPress Blog

Since Twitter became popular, many bloggers like to display recent twitter tweets on their website or blog. To display most recent twitter tweets, simply copy and paste the following code to your sidebar.php file.

<?php
// Your twitter username.
$username = "TwitterUsername";
// Prefix - some text you want displayed before your latest tweet.
// (HTML is OK, but be sure to escape quotes with backslashes: for example href=\"link.html\")
$prefix = "";
// Suffix - some text you want display after your latest tweet. (Same rules as the prefix.)
$suffix = "";
$feed = "http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=1";
function parse_feed($feed) {
$stepOne = explode("<content type=\"html\">", $feed);
$stepTwo = explode("</content>", $stepOne[1]);
$tweet = $stepTwo[0];
$tweet = str_replace(”&lt;”, “<”, $tweet);
$tweet = str_replace(”&gt;”, “>”, $tweet);
return $tweet;
}
$twitterFeed = file_get_contents($feed);
echo stripslashes($prefix) . parse_feed($twitterFeed) . stripslashes($suffix);
?>

Thanks the awesome code from WP Hacks.

How to Create Meta Description Function and Meta Keyword Tags

If you want to create meta description function and meta keyword tags for your wordpress themes, open header.php file and then copy and paste the following code within <head> and </head>.

<meta name="description" content="
<?php if ( (is_home()) || (is_front_page()) ) {
echo ('Your main description goes here');
} elseif(is_category()) {
echo category_description();
} elseif(is_tag()) {
echo '-tag archive page for this blog' . single_tag_title();
} elseif(is_month()) {
echo 'archive page for this blog' . the_time('F, Y');
} else {
echo get_post_meta($post->ID, "Metadescription", true);
}?>">

Thanks the original function from Malcolm Coles.

How to Display the Total Number of Comments on Your Blog

Who don’t like to have comments on your blog? If your blog has many comments, it can be say that your blogs is popular, at least people like to interact with you to ask question and share information with you. If you really proud with your total number of comments on your blog, and want to show off to visitors.

To display the total number of comments on your blog, simple add the following code to your wordpress files.

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

Special thanks to PHP Magic Book for this awesome code.

How to Create Page Templates

Normally I will use page templates to display the archives which will list all posts on my blog, anyway you can use page templates to get specialized results apart from the basic static page template.

What you need to do is to add the following code to the first line of your php files, you can change the template name as you wish.

<?php
/*
Template Name: Archives
*/
?>

So now, you have to create a page template named Archives. Then, create a new page, find Template drop down box (refer picture) and choose Archives, this is a page template named Archives that you have created just now.

How to Add a Print Button to Your Posts

I don’t think you will need to add a print button to your posts on your blog, but in case you want to have it, you can just copy and paste the following code into single.php file. It’s looks simple, right? @_@

<a href="javascript:window.print()">Print this Article</a>

How to Display Google Adsense After The First Post

If you want to display Google Adsense After the first post, then you can refer to the following code and see how it works. Honestly, this is a wonderful code that I have been looking for so long.

<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); $loopcounter++; ?>

// the loop stuffs

<?php if ($loopcounter <= 1) { include (TEMPLATEPATH . '/ad.php'); } ?>

<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>

Thanks Web Designer Wall for this awesome code.