0

I'm trying to create a related post section in my wordpress custom theme. The objective here is if the user views a post to a certain category it will also show all the post from that category below. I have attached an image below.

http://i255.photobucket.com/albums/hh140/testament1234/relatedposts_zpsa830adfc.jpg

How do i achieve this? Do i use wp_query just like this?

<?php query_posts('cat=6', 'posts_per_page=-4=-'); if(have_posts()) : while(have_posts()) :the_post(); ?>

The code above sets what category it obtains the post. 'cat'

Solution of kwncc

    <div id="post-container" class="eleven columns alpha omega post">

         <?php $postCategories = ''; while ( have_posts() ) : the_post(); ?>
         <h2 class="post-title"><?php the_title() ?></h2>

         <?php setPostViews(get_the_ID()); ?> <!-- Set Post Views -->

         <ul class="meta-icons-large">
            <li id="meta-author-large"><span><?php the_author_posts_link() ?> /</span></li>
            <li id="meta-categories-large"><span><?php the_category(', ') ?> /</span></li>
            <li id="meta-comments-large"><span><?php comments_number() ?> /</span></li>
            <li id="meta-date-large"><span><?php the_time('F jS, Y') ?> /</span></li>
            <li id="meta-views-large"><span><?php echo getPostViews(get_the_ID()); ?></span></li> <!-- Display Post Views -->
         </ul>

         <?php the_content(); ?>

         <?php endwhile; ?>
    </div>


    <div id="related-posts-container" class="eleven columns alpha omega related-post">
         <h2>related posts</h2>

         <?php $postCatIds = ''; foreach($postCategories as $catIndex => &$catValue){
            $postCatIds .= $category->cat_ID; if( $catIndex < (count($postCategories)-1)){ $postCatIds .= ', ';
                       }
            }
            ?>

         <?php query_posts('cat='.$postCatIds, 'posts_per_page=4'); if(have_posts()) : while(have_posts()) { the_post(); } ?>

         <div id="related-post-thumbnail-container" class="three columns alpha related-posts">

             <h3><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>

         </div>

          <?php endwhile; endif; wp_reset_query();?>

   </div>

   <div id="comments-container" class="eleven columns alpha omega comments">
        <?php comments_template('', true); ?>
   </div>


</div>

2 Answers 2

1

I'd suggest you don't load related posts on page load because this will dramatically hurt your website's performance.

The free WordPress plugin Related Posts for WordPress automatically finds related posts (amongst others based on title) and caches them for you, offering you real related posts without hurting your website's performance. After the automatic linking is done you can, if required, manually add, edit or delete related posts. The plugin also comes with template tags so you can display the related posts anywhere in your custom theme you want. Simply use rp4wp_children() in your theme where you want the related posts to be displayed.

You can give it a try via the WordPress.org repo: http://wordpress.org/plugins/related-posts-for-wp/

Sign up to request clarification or add additional context in comments.

Comments

0

This page consists of 2 different loops: 1. for the main content - article of the post 2. for the related posts.

For the first part, you have just to include the loop. In addition, since you want to find the related to this article posts you have to also get the current post categories.

PHP code:

$postCategories = '';
if(have_posts()) : while(have_posts()){
   the_post();
   $postCategories = get_the_category();
   ...
}

Then for the second part where you want to include the related posts you can get the category IDs related to the current post and create the query you need.

// get comma separated category IDs
$postCatIds = ''; 
foreach($postCategories as $catIndex => &$catValue){

   $postCatIds .= $category->cat_ID;
   if( $catIndex < (count($postCategories)-1)){
     $postCatIds .= ', ';
   }
}

//query the related categories posts
query_posts('cat='.$postCatIds, 'posts_per_page=4'); 
if(have_posts()) : while(have_posts()) {
   the_post();
   ...
}

Hope that helps!

3 Comments

Sorry but i'm not yet adept in wordpress development. Basically this code <?php $postCategories = ''; while ( have_posts() ) : the_post(); ?> will obtain the category of the post
Not sure if i got it right. Still getting some errors on your solution
$postCategories = get_the_category(); this line will get the categories that your post is assigned to. So if your post is assigned to category1, category2, category3, it will get this 3 categories. Having obtained these categories of your current post you can later retrieve "related" posts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.