How to list related posts in WordPress no plugin, just code!

This article was written by in October 1, 2013, & may not be posted on other sites!
Original source url for this article: How to list related posts in WordPress no plugin, just code!

How to list related posts WordPress no plugin, just code!
On single post pages you might want to add code to list related posts by tags or category without using a wordpress plugin. We are working with the single.php file to list related posts on a single post page. This is how to:

 

List categories as links from current post to related posts

Use this code in single.php. The code will list current posts categories as links, these links will take the visitor to a category page listing all related post using the same categories. Use this code inside the loop in single.php.
<?php the_category(); ?>

You can alter the outcome by altering the code
<?php the_category( $separator, $parents, $post_id ); ?>
$separator =  The character separating the categories, default is in an unordered list
$parents = To add parrents, Use 'multiple' to display separate links to parent and child categories, exhibiting “parent/child” relationship. Or use 'single' to display link to child category only, with link text exhibiting “parent/child” relationship.
$post_id= Default is to get categories of current post, You can also retrieve categories from a specific post ID.
How to list related posts in WordPress no plugin, just code!

 

List categories as links from current post to related posts writing your own code

Use this code in single.php. The code will list current posts categories as links, these links will take the visitor to a category page listing all related post using the same categories. The outcome is similar to first example above, but you can alter this code as you wish giving more options on how to show the categories.  Use this code inside the loop in single.php.
<div id="related-posts" ?>
<?php
$categories = get_the_category($post->ID);
$separator = ' ';
$output = '';
if($categories){
foreach($categories as $category) {
$output .= '<a href="'.get_category_link( $category->term_id ).'" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '">'.$category->cat_name.'</a>'.$separator;
}
echo trim($output, $separator);
}
?>
</div>

 

List tags as links from current post to related posts

Use this code in single.php. The code will list current post tags as links, these links will take the visitor to a tags page listing all related posts using the same tags. Use this code inside the loop in single.php.
<?php the_tags(); ?>

How to list related posts in WordPress no plugin, just code!

You can alter the outcome by altering the code
<?php the_tags( $before, $sep, $after ); ?>
$before =  The word to display before the tags, default is “Tags”
$sep = The character separating the tags, default is a comma
$after = Text to display after listed tags, default is to display nothing

 

List 3 related posts by category, as individual links to each post

How to list related posts in WordPress no plugin, just code!

Use this code in single.php. The code will list 3 related posts in the same category as current post. These links will take the visitor directly to the individual posts. Use this code inside or outside the loop in single.php.
<h1>Related posts</h1>
<div>
<ul>
<?php
$related = get_posts( array( 'category__in' => wp_get_post_categories($post->ID), 'numberposts' => 3, 'post__not_in' => array($post->ID) ) );
if( $related ) foreach( $related as $post ) {
setup_postdata($post); ?>
<li>
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>
</li>
<?php }
wp_reset_postdata(); ?>
</ul>
</div>

 

List 3 related posts by tags, as individual links with title and thumbnail image

How to list related posts in WordPress no plugin, just code!

Use this code in single.php. The code will list 3 related posts in the same tags as current post. Related links are displayed as the post title + thumbnal image linking the visitor directly to the individual posts. Use this code inside the loop in single.php.
<div class="related-posts">  
<h3>Related posts</h3>  
<?php  
    $orig_post = $post;  
    global $post;  
    $tags = wp_get_post_tags($post->ID);  
      
    if ($tags) {  
    $tag_ids = array();  
    foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;  
    $args=array(  
    'tag__in' => $tag_ids,  
    'post__not_in' => array($post->ID),  
    'posts_per_page'=>3, // Number of related posts to display
    'caller_get_posts'=>1  
    );  
      
    $my_query = new wp_query( $args );  
 
    while( $my_query->have_posts() ) {  
    $my_query->the_post();  
    ?>  
      
    <div>  
        <a rel="external" href="<? the_permalink()?>"><?php the_post_thumbnail('thumbnail'); ?><br />  
        <?php the_title(); ?>  
        </a>  
    </div>  
      
    <? }  
    }  
    $post = $orig_post;  
    wp_reset_query();  
    ?>  
</div>

Spread the word

Facebooktwittergoogle_pluslinkedinmail

Leave a Reply

Your email address will not be published. Required fields are marked *