WordPress ‘hacks’
Been doing a bit of WordPress customisation work recently, – here are a few ways to delve into your content. There are plugins available for a lot of these techniques; however sometimes you may wish to trim your plugins for speed or other considerations. Enjoy.
1) Displaying Related Posts
Related posts can retain your readers by offering them easy to click, context related links. To execute this ‘hack’ you will need to edit your single.php file (in your current theme)
<?php
//Place in the loop to list 5 posts related to the first tag in the current post
$tags = wp_get_post_tags($post->ID);
if ($tags) {
echo 'Related Posts';
$first_tag = $tags[0]->term_id;
$args=array(
'tag__in' => array($first_tag),
'post__not_in' => array($post->ID),
'showposts'=>5, 'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
<?php endwhile;
}
}
?>
2) Displaying Recent Comments
Recent comments can be very helpful to build awareness of what topics readers are finding value in. To display recent comments you will need to modify your functions.php file (in your current theme)
If this file is not present, you will need to create it.
<?php
function recent_comments($src_count=10, $src_length=60, $pre_HTML='<ul>', $post_HTML='') {
global $wpdb;
$sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID, comment_post_ID, comment_author, comment_date_gmt, comment_approved, comment_type,
SUBSTRING(comment_content,1,$src_length) AS com_excerpt 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 $src_count";
$comments = $wpdb->get_results($sql);
$output = $pre_HTML;
foreach ($comments as $comment) {
$output .= "<li><a href=\"" . get_permalink($comment->ID) . "#comment-" . $comment->comment_ID . "\" title=\"on " . $comment->post_title . "\">" . strip_tags($comment->com_excerpt) ."...</a></li>";
}
$output .= $post_HTML;
echo $output;
}
?>
Wherever you would like to place comments simply include the following line:
<?php recent_comments(); ?>
3) Adding A Print Button To Blog Posts
Of course there are keyboard shortcuts and other ways to perform this task, but it is nice little feature to offer.
Edit your single.php file (in your current theme) and add the following code:
<a href="javascript:window.print()" rel="nofollow">Print this Article</a>
4) Excluding Categories From Your RSS Feed
Perhaps your have a category which has little to do with the rest of your blog, – sometimes it can be useful to exclude certain categories from your feed. Simple.
You will need to know the Category ID (cat_id= seen in your URLs)
Edit your function.php file (for your current theme, or create one if it doesnt exist)
function myFilter($query) {
if ($query->is_feed) {
$query->set('cat','-5'); //Don't forget to change the category ID =^o^=
}
return $query;
}
add_filter('pre_get_posts','myFilter');