.. Your Daily Design Dosis

My favorite Hacks for WordPress

| 25 Comments

There are probably times when you come across a nice feature in a blog, and you just start thinking: How can I get this in my WordPress blogging site as well. Everybody has experienced this feeling, I know I did.

Sometimes you know you want that specific feature, but don’t know where to look for, or even what to look for. In this article I will be sharing some of my most favoured WordPress Hacks that you will definitely find useful.

Display Most Recent Comments

Have you seen sites that display the most recent comments in their sidebar.php or footer.php? Well this can be done easily with these codes. Simply paste the following code anywhere you want to display the most recent comments.

<?php global $wpdb; $sql = "
SELECT DISTINCT ID, post_title, post_password, comment_ID,comment_post_ID, comment_author, comment_date_gmt, comment_approved,comment_type,comment_author_url,
SUBSTRING(comment_content,1,55) AS com_excerpt
<!-- 55 characters set for length -->
FROM $wpdb->comments
LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID =$wpdb->posts.ID)
WHERE comment_approved = '1' AND comment_type = '' AND post_password = ''
ORDER BY comment_date_gmt DESC
LIMIT 6";$comments = $wpdb->get_results($sql);$output = $pre_HTML$output .= "\n<ul>";
<!-- Limit amount of comments is set to 6 comments -->
foreach ($comments as $comment) {$output .= "\n<li><i>".strip_tags($comment->comment_author)."</i> : " . "<a href=\"" . get_permalink($comment->ID) .
"#comment-" . $comment->comment_ID . "\" title=\"on " .$comment->post_title . "\">" . strip_tags($comment->com_excerpt)."</a> [~]</li>";}$output .= "\n</ul>"
$output .= $post_HTML;
echo $output;?>

Display The Most Popular Posts

Just paste the following code in your template file where you want to display the most commented posts (eg. sidebar.php). To change the number of displayed posts, simply change ‘5’ in the code.

<ul> <?php $result = $wpdb->get_results("SELECT comment_count,ID,post_title FROM $wpdb->
posts ORDER BY comment_count DESC LIMIT 0 , 5"); <!-- number of displayed posts -->
foreach ($result as $post) { setup_postdata($post); $postid = $post->ID; $title = $post->post_title; $commentcount = $post->comment_count; if ($commentcount != 0)
{ ?> <li><a href="<?php echo get_permalink($postid); ?>" title="<?php echo $title ?>">
<?php echo $title ?></a> {<?php echo $commentcount ?>}</li> <?php } } ?> </ul>

Add Related Posts in your Pages

This code will display related posts based on the current post tag(s). Make sure you paste this within the loop

<?php $original_post = $post; $tags = wp_get_post_tags($post->ID); if ($tags) {
echo '<h4>More Posts about this Topic:</h4>'; $first_tag = $tags[0]->term_id; $args=array('tag__in' => array($first_tag), 'post__not_in' => array($post->ID),
'showposts'=>5,  <!-- number of displayed posts -->
'caller_get_posts'=>1;
$my_query = new WP_Query($args); if( $my_query->have_posts() ) {echo "<ul>";while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?> </a></li>
<?php endwhile; echo "</ul>";
} }
$post = $original_post; wp_reset_query();  ?>

Display The Number Of Search Results

Open search.php, copy and paste the following code where you want this to be in your layout. Make sure you paste this within the loop.

<p>Search Result for: "<?php /* Search Count */ $allsearch = &new WP_Query("s=$s&showposts=-1"); $key = wp_specialchars($s, 1); $count = $allsearch->post_count; _e(''); _e('<b>'); echo $key; _e('</b>');_e('"'); _e(' &mdash; '); echo $count . ' '; _e('articles'); wp_reset_query(); ?>

Remove the Nofollow from your Comments

By default, WordPress automatically converts all links from the comments to nofollow. If you prefer your links to be dofollow, just read and use the following recipe. Copy the following code, and paste it on the functions.php file from your theme. Once you saved the file, the rel="nofollow" attributes will be removed.

function remove_nofollow($string) {
$string = str_ireplace(' rel="nofollow"', '', $string);
return $string;
}
add_filter('the_content', 'remove_nofollow');

wordpress-code-snippets

Display a Retweet Button On Your Site

With Twitter getting so much exposure, as a blogger you should already be using it to your advantage. Power of twitter is like no other because it is word of mouth advertising. To make this easier on your readers, what you can do is place a prominent retweet button, so they can retweet the article with one click. Not only just that, but you should make it the way so you can track the retweets as well. That is where tweetmeme code comes in.

In this tutorial we will have you create a button that will link to the text in the following format:

RT @yoursitename Title of the Post – Link

Add the following code in the template file of your choosing most likely single.php

For the Large Button:

<script type="text/javascript">tweetmeme_source = 'gonzodesign_';</script>
<script type="text/javascript" src="http://tweetmeme.com/i/scripts/button.js"> </script>

For the Compact Button:

<script type='text/javascript'>tweetmeme_style = "compact";tweetmeme_source = 'gonzodesign_';
</script>

Remember to change the source to your twitter account name, this way you will not only promote your account to get more followers, but your article will be promoted as well.

Display Archives in a Drop Down Menu

Some blogs have a lot of archives which they can’t display in their sidebar.php. Or some bloggers do not want to take up a lot of space displaying the (e.g. monthly) archives. This option allows them to have a select menu, dropdown menu, for categories.

<ul>
<li>
<select name="archive-dropdown" onchange='document.location.href=this.options[this.selectedIndex].
value;'><option value=""><?php echo attribute_escape(__('Select Month')); ?></option>
<?php wp_get_archives('type=monthly&format=option&show_post_count=1'); ?> </select></li>
</ul>

Modify Excerpt Length and More Tags

WordPress lets you display Excerpts, but up until version 2.9 you could not control the excerpt length. With the following code, you can increase the length from default 55 words to as many words as you like. Open your functions.php file and add the following codes in there:

// custom excerpt length
function custom_excerpt_length($length) {
return 100;
}
add_filter('excerpt_length', 'custom_excerpt_length');

Change the 100 word limit to the count that you desire and make sure to add the following code as well into your functions.php

// custom excerpt ellipses for 2.9+
function new_excerpt_more($more) {
return '…';
}
add_filter('excerpt_more', 'new_excerpt_more');

A [read more …] link at the end of your excerpts

Another great hack for you functions.php, this function allows you to insert a text at the end of your excerpts, click to read the whole article.

 // no more jumping for read more link
function no_more_jumping($post) {
return '<a href="'.get_permalink($post->ID).'">'.' [ <em>read more ..</em> ]'.'</a>';
}
add_filter('excerpt_more', 'no_more_jumping');

Display Word Count Of The Post

Open single.php and paste the following code where you want to display the word count.

<!-- Display your posts word count -->
{<em>This article contains </em>: <?php echo wcount(); ?> words}

In your function.php you need to paste also the following code to make this hack work:

/* Display your posts word count */
function wcount(){
ob_start();
the_content();
$content = ob_get_clean();
return sizeof(explode(" ", $content));
}

Conclusion

There are loads of ways to customize your WordPress-theme, you need to be a bit handy with programming/coding in PhP, HTML and CSS.

Note: These hacks worked at the time they were published, but as new versions of WordPress are released, some may no longer work.  Please backup your theme before attempting any hacks so you can restore things if something goes wrong.

Resources

– § –

We hoped that you enjoyed this long and resourceful article. If you like it please retweet, and share it with your friends on Facebook. If you think I’ve missed a very special or great Hack, please let me know by filling in a comment … cheers!

 

Author: Jan Rajtoral

Jan Rajtoral AKA Gonzo the Great is the Founder of and Designer at gonzodesign, providing design services across the full spectrum of Brand Identity, Graphic Design, Print and Advertising Design & Website Design.

25 comments

on this article: “My favorite Hacks for WordPress”
  1. Hi Aron,

    thanks for your comment, glad you liked the article! Cheers & Ciao ..

  2. Hi HLW,

    You’re more than welcome, enjoy the hacks!
    Cheers & Ciao ..

  3. Wow, that is an impressive list of hacks! Thanks a lot for sharing.
    .-= How Lose Weight´s last blog ..How To Lose Weight AND Get Rid Of Cellulite Eating Plan =-.

  4. Hi Kad1r,

    Internet ‘lives’ by the fact that websites connect by linking to each other, without linking there is no internet!

    Allow the internet to be one big community, remove the ‘nofollow’ from your comments!

    Thanks for your comment, Cheers & Ciao ..

  5. Hi ielts exam,

    First of all, I hope my English is good? Saw your website ;-P

    Thanks for dropping by and commenting, Cheers & Ciao ..

  6. Hi EMT,

    thanks for your comment, much appreciated! Cheers & Ciao …