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.

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 Seperate Comments and Trackbacks

Most wordpress themes combine comments with trackbacks, if you want to separate comments and trackbacks so that it will looks more organized, then you can follow the below step by step guide.

Step 1

Open comments.php and look for

<?php foreach ($comments as $comment) : ?>

Paste after

<?php $comment_type = get_comment_type(); ?>
<?php if($comment_type == 'comment') { ?>

Step 2

Scroll down a bit and look for

<?php endforeach; /* end for each comment */ ?>

Paste before

<?php } else { $trackback = true; } /* End of is_comment statement */ ?>

Step 3

Look for

<?php else : // this is displayed if there are no comments so far ?>

Paste before

<?php if ($trackback == true) { ?>
<h3>Trackbacks</h3>
<ol>
<?php foreach ($comments as $comment) : ?>
<?php $comment_type = get_comment_type(); ?>
<?php if($comment_type != 'comment') { ?>
<li><?php comment_author_link() ?></li>
<?php } ?>
<?php endforeach; ?>
</ol>
<?php } ?>

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>

How to Exclude Certain Categories

There are 2 ways to exclude certain categories from your blog, one is use wordpress plugin named Advanced Category Excluder. This plugin was born because there was a no other real alternative to enable content sparationd and some CMS like functionalities in WordPress. The main goal was, to enhance WordPress’s functionalities, to hide some unwanted categories, from defined parts of the blog.

The alternative way is without plugin, you can add the below code inside the loop then it will work.

<?php
if ( have_posts() ) : query_posts($query_string .'&cat=-1,-2'); while ( have_posts() ) : the_post();
?>