Published in

Blog engine or CMS, call it whatever you want but WordPress is a great tool for web designers. It just get a little hard to use sometimes when you want to do some more advanced things in your theme and you are not a coder. Here are 8 snippets of code that have helped me in recent projects.

1. Include post thumbnail in RSS feed

If you are using WordPress thumbnails in your theme, you may want to include it in your RSS feed. To do so, just include the following code in your function.php file.

function insertThumbnailRSS($content) {

global $post;

if ( has_post_thumbnail( $post->ID ) ){

$content = '<p>' . get_the_post_thumbnail( $post->ID, 'medium' ) . '</p>' . $content;

}

return $content;

}

add_filter('the_excerpt_rss', 'insertThumbnailRSS');

add_filter('the_content_feed', 'insertThumbnailRSS');

Recipe found here.

2. Next & Previous Post Titles With Thumbnails

Another thing you may want to do if you are using thumbnails is to display the next and previous posts thumbnails with their titles.

For the previous post’s thumbnail and title use this code

<?php previous_post_link('%link »','%title', TRUE); ?>

<?php $prevPost = get_previous_post(true); $prevthumbnail = get_the_post_thumbnail($prevPost->ID, array(150,150) ); echo $prevthumbnail; ?>

For the next post’s thumbnail and title use this code.

<?php next_post_link('« %link','%title', TRUE); ?><br /><?php $nextPost = get_next_post(true); $nextthumbnail = get_the_post_thumbnail($nextPost->ID, array(150,150) ); echo $nextthumbnail; ?>

Recipe found here.

3. Search Specific Category in WordPress

If you have community news or a side blog on your website, this could be useful to have a search specific to some areas of the blog.

<form method="get" id="searchform" action="<?php bloginfo('home'); ?>/">

<div><input type="text" value="<?php echo wp_specialchars($s, 1); ?>" name="s" id="s" />

<input type="hidden" name="cat" value="22" />

<input type="submit" id="searchsubmit" value="Search" />

</div>

</form>

Recipe found here.

4. List top 10 authors of the blog with their last post

For multi-author blogs, especially with many authors, you will maybe want to list the most active users with their last posts. To achieve that include the following code in your archive.php or any other file where you want to display your archives.

<?php

$uc=array();

$blogusers = get_users_of_blog();

if ($blogusers) {

foreach ($blogusers as $bloguser) {

$post_count = get_usernumposts($bloguser->user_id);

$uc[$bloguser->user_id]=$post_count;

}

arsort($uc); //use asort($uc) if ascending by post count is desired

$maxauthor=2;

$count=0;

foreach ($uc as $key => $value) {

$count++;

if ($count <= $maxauthor) {

$user = get_userdata($key);

$author_posts_url = get_author_posts_url($key);

$post_count = $value;

echo 'User ID ' . $user->ID . ' ' . $user->user_firstname . ' ' . $user->user_lastname . ' number of posts: ' . $post_count . ' author posts url: ' . $author_posts_url .'';

$args=array(

'showposts'=>1,

'author'=>$user->ID,

'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><small><?php the_time('m.d.y') ?></small> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>

<?php endwhile; } } } } ?>

Recipe found here.

5. Automatically insert content in your RSS feed

Do you want to add a signature in the bottom of every of your articles? It’s quite easy, for this just add the following code in your functions.php file.

function insertFootNote($content) {

if(!is_feed() && !is_home()) {

$content.= "<div class='subscribe'>";

$content.= "<h4>Enjoyed this article?</h4>";

$content.= "<p>Here is some extra content!</p>";

$content.= "</div>"; } return $content; }

add_filter ('the_content', 'insertFootNote');

Recipe found here.

6. Display a Comment’s Number in a List

Open comments.php and scroll down to where the comments loop begins:

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

Before the loop begins, we need to set the PHP variable we’ll use to determine the number. Change the above line to the following:

<?php $commentNum = 1; foreach ($comments as $comment) : ?>

We’ve now assigned the value “1″ to the variable $commentNum. Next, find the end of the loop:

<?php endforeach; ?>

We need to increase the value of $commentNum by 1 before the loop ends. To do this, we use PHP’s increment operator:

<?php $commentNum++; endforeach; ?>

Now you can use the variable $commentNum anywhere within the loop. Simply echo the variable with PHP to display the current comment’s number.

<?php echo $commentNum; ?>

Recipe found here.

7. Display your most commented posts in your blog sidebar

Displaying most commented posts in the sidebar is a good way to get more comment (at least I read this somewhere). Just include this code in your sidebar.php file.

<h2>Popular Posts</h2>

<ul>

<?php $result = $wpdb->get_results("SELECT comment_count,ID,post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 5");

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>



Recipe found here.

8. Excluding Posts from Your WordPress Feed

This will typically used when you have a sideblog on your blog, for example to avoid overwhelming your RSS subscribers. If you want to exclude posts from a specific category, use the following code in your functions.php file.

function myFilter($query) {

if ($query->is_feed) {

$query->set('cat','-5'); }

return $query; }

add_filter('pre_get_posts','myFilter');

Recipe found here.