How to show related articles without any wordpress plugin using category

How to show related articles without any wordpress plugin using category. In many blogs people are showing the related or linked articles. Showing similier or related articles will increaze your site SEO. Showing related article in wordpress site is always good for users to attract the visitors.

More visitor will visit your site. For showing the related articles you need copy following code into your functions.php file.

// "Similier Articles"
function wpapi_more_from_cat( $title = "Similier Articles:" ) {
    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 = '<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>';
    }
    echo $output;
}

After that open your single.php file and use following code.

wpapi_more_from_cat();

Using above code you can show the related articles in your wordpress site.

You may like following Articles!

  • http://dolarki.com.pl Noclegi mpk Lodz

    Hello There. I found your blog using msn. This is an extremely well written article.
    I’ll be sure to bookmark it and return to read more of your useful information.
    Thanks for the post. I’ll definitely comeback.

  • jamielt

    Thanks for the helpful tutorial! Easy to implement and best of all, it works.

  • Filip

    can you make it display thumbnails as well?

  • http://www.themesrefinery.com/ Themesrefinery

    Really such a nice work i did the same thing on my website.Thanks