Title: Show &#8220;Related Posts&#8221; before comment section
Last modified: June 2, 2017

---

# Show “Related Posts” before comment section

 *  [xanderlous](https://wordpress.org/support/users/xanderlous/)
 * (@xanderlous)
 * [9 years ago](https://wordpress.org/support/topic/show-related-posts-before-comment-section/)
 * _[ Moderator note: [moved to Fixing WordPress](https://wordpress.org/support/topic/before-posting-in-everything-wordpress/?view=all).]_
 * Hi there,
 * I just added code into my single.php to show “related posts” at the end of a 
   post. However, I do not seem to place this section above the comment section.
   If I place it before the comment it will return a syntax error.
 * Any ideas how to fix this?
 * I am using this code for related posts:
 *     ```
       <div class="relatedposts">
       <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'=>4, // 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 class="relatedthumb">
           <a rel="external" href="<? the_permalink()?>"><?php the_post_thumbnail(array(150,100)); ?><br />
           <?php the_title(); ?>
           </a>
         </div>
   
         <? }
         }
         $post = $orig_post;
         wp_reset_query();
         ?>
       </div>
       ```
   
 * The current total code in single.php currently looks like this:
 *     ```
       <?php
       /**
        * The template for displaying all single posts
        *
        * @link https://developer.wordpress.org/themes/basics/template-hierarchy/#single-post
        *
        * @package WordPress
        * @subpackage Twenty_Seventeen
        * @since 1.0
        * @version 1.0
        */
   
       get_header(); ?>
   
       <div class="wrap">
       	<div id="primary" class="content-area">
       		<main id="main" class="site-main" role="main">
   
       			<?php
   
       				/* Start the Loop */
       				while ( have_posts() ) : the_post();
   
       					get_template_part( 'template-parts/post/content', get_post_format() );
   
       				// If comments are open or we have at least one comment, load up the comment template.
       					if ( comments_open() || get_comments_number() ) :
       						comments_template();
       					endif;
   
       				endwhile; // End of the loop.
       			?>
       <div class="relatedposts">
       <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'=>4, // 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 class="relatedthumb">
           <a rel="external">"><?php the_post_thumbnail(array(150,100)); ?><br />
           <?php the_title(); ?>
           </a>
         </div>
   
         <? }
         }
         $post = $orig_post;
         wp_reset_query();
         ?>
       </div>
       		</main><!-- #main -->
       	</div><!-- #primary -->
       	<?php get_sidebar(); ?>
       </div><!-- .wrap -->
   
       <?php get_footer();
       ```
   
    -  This topic was modified 9 years ago by [xanderlous](https://wordpress.org/support/users/xanderlous/).
    -  This topic was modified 9 years ago by [Jan Dembowski](https://wordpress.org/support/users/jdembowski/).

Viewing 1 replies (of 1 total)

 *  Thread Starter [xanderlous](https://wordpress.org/support/users/xanderlous/)
 * (@xanderlous)
 * [9 years ago](https://wordpress.org/support/topic/show-related-posts-before-comment-section/#post-9193464)
 * Any ideas anyone?

Viewing 1 replies (of 1 total)

The topic ‘Show “Related Posts” before comment section’ is closed to new replies.

## Tags

 * [comment](https://wordpress.org/support/topic-tag/comment/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 1 participant
 * Last reply from: [xanderlous](https://wordpress.org/support/users/xanderlous/)
 * Last activity: [9 years ago](https://wordpress.org/support/topic/show-related-posts-before-comment-section/#post-9193464)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
