Latest Updates » [1] Auto XML Sitemap [2] Create Contact Page [3] Blogger and Wordpress Templates! Download Now !


Tuesday, 9 July 2013

WPTRICK : Auto Related Posts at end of WordPress Post



When a reader finished reading one of your blog posts, It is the better idea to suggesting him other interested post he might like as well?

It will also increase your blog page views and engage your reader to spend more time on your blog.

Related post also help to reduce your bounce rate and increase Alexa rankings. Here’s a WordPress trick to automatically display related posts (based on category) below the current post.

WPTrick: Automatically related post at end of wordpress post based on categories..

Step 1. Open you functions.php file and paste following code in to it..

// "More from This Category" list by Barış Ünver @ Wptuts+
function wptuts_more_from_cat( $title = "More From This Category:" ) {
    global $post;
    // We should get the first category of the post
    $categories = get_the_category( $post->ID );
    $first_cat = $categories[0]->cat_ID;
    // Let's start the $output by displaying the title and opening the <ul>
    $output = '<div id="more-from-cat"><h3>' . $title . '</h3>';
    // The arguments of the post list!
    $args = array(
        // It should be in the first category of our post:
        'category__in' => array( $first_cat ),
        // Our post should NOT be in the list:
        'post__not_in' => array( $post->ID ),
        // ...And it should fetch 5 posts - you can change this number if you like:
        'posts_per_page' => 5
    );
    // The get_posts() function
    $posts = get_posts( $args );
    if( $posts ) {
        $output .= '<ul>';
        // Let's start the loop!
        foreach( $posts as $post ) {
            setup_postdata( $post );
            $post_title = get_the_title();
            $permalink = get_permalink();
            $output .= '<li><a href="' . $permalink . '" title="' . esc_attr( $post_title ) . '">' . $post_title . '</a></li>';
        }
        $output .= '</ul>';
    } else {
        // If there are no posts, we should return something, too!
        $output .= '<p>Sorry, this category has just one post and you just read it!</p>';
    }
    // Let's close the <div> and return the $output:
    $output .= '</div>';
    return $output;
}

Step 2. Now open your single.php file and paste codes as shown below, where you'd like to display the related posts:

<?php echo wptuts_more_from_cat( 'More From This Category:' ); ?>

I would like to thank wp-tuts for this code of snippet. Pl consider it sharing with friends if you liked this quick tip.


Kindly Bookmark this Post using your favorite Bookmarking service:
Technorati Digg This Stumble Stumble Facebook Twitter

Advertise Here

0 comments:

Post a Comment


ABOUT THE AUTHOR

Hi! I am K Ajay, currently live in Chandigarh, India. A young Web Designer, Pro Blogger, Graphic Designer & an addicted Web Developer. I love working on Blogger and WordPress using CSS Tricks,and Web Designing. Read More...

FOLLOW US HERE


RANDOM POSTS

Bwidgets4all-Helping Bloggers with Blogging Tricks © 2013. All Rights Reserved | Free Blogger Templates | | Back to Top