• xanderlous

    (@xanderlous)


    [ Moderator note: moved to Fixing WordPress. ]

    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();
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)

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