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 Twitter Count in Plain Text

Many people will use Twitter counter badge from TwitterCounter.com to show twitter count in their website or blog, but twitter counter badges from TwitterCounter.com didn’t come with color personalization, sometimes it can’t match the color of the theme, so I don’t like to use it.

If you want to display Twitter count in plain text, you can follow the below steps.

1. Add the following code to functions.php.

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;
}

2. Copy and paste the following short code to anywhere on your theme files, remember replace your twitter username.

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

How to Display Categories in Drop-Down Box

Personally, I prefer to display my blog categories without in drop down box, but if you really have many categories on your blog, perhaps you can display categories in drop-down box so that your blog will looks more neat.

Below is the simple code to display categories:-

<h2>Categories</h2>
<ul>
<?php wp_list_cats('sort_column=name'); ?>
</ul>

To display categories in drop-down box, use the following code.

<form action="<?php bloginfo('url'); ?>/" method="get">
<?php
$select = wp_dropdown_categories('show_option_none=Select category&show_count=1&orderby=name&echo=0');
$select = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select); echo $select; ?>
<noscript><input type="submit" value="View" /></noscript>
</form>