• How can I change related post by category to related post by tags ?
    This is my code, i get it from related-post.php. I am using Wiles theme

    <?php
    /** Latest Posts from the same Category
     * @package wiles **/
    global $wiles_option;
    $categories = get_the_category( $post->ID );
    $first_cat ='';
    if(isset($categories[0]->cat_ID)){
    $first_cat = $categories[0]->cat_ID;
    }
    $posts_to_show =  $wiles_option['single_related_posts_to_show'];
    $args = array(
    	'category__in' => array( $first_cat ),
    	'post__not_in' => array( $post->ID ),
    	'posts_per_page' => 6,
      'orderby' => 'rand'
    );
    $related_posts = get_posts( $args );
    if( $related_posts ) {
    ?>
    	<section class="single-box related-articles">
            <h4 class="entry-title">
              <?php _e('You May Also Like', 'wiles'); ?>
            </h4>
    			<?php foreach( $related_posts as $post ): setup_postdata( $post ); ?>
    			<div class="related-article four-col">
                      <figure class="entry-image">
                          <a href="<?php the_permalink(); ?>">
    						  <?php
                              if ( has_post_thumbnail() ) {
                                  the_post_thumbnail( 'small');
                              } else { ?>
                              <img src="<?php  echo get_template_directory_uri(); ?>/images/default-image.png" alt="<?php the_title();  ?>" />
                              <?php } ?>
                          </a>
                      </figure>
                              <h5><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h5>
                  </div>
        		<?php endforeach; ?>
    	</section><!-- .single-box .related-posts -->
    <?php
    } else {
    	return;
    }
    wp_reset_postdata();
    ?>

Viewing 2 replies - 1 through 2 (of 2 total)
  • I’ve replaced your loop so that posts become related via the tags they have:

    global $post;
    $tags = wp_get_post_tags($post->ID);
    if ($tags) {
    	$tag_ids = array();
    	foreach($tags as $tag) $tag_ids[] = $tag->term_id;
    	$args = array('tag__in' => $tag_ids, 'post__not_in' => array($post->ID), 'posts_per_page' => 6, 'orderby' => 'rand');
    	$related = new wp_query($args);
    	if ($related->have_posts()) { ?>
    		<section class="single-box related-articles">
    			<h4 class="entry-title"><?php
    				_e('You May Also Like', 'wiles'); ?>
    			</h4><?php
    			while ($related->have_posts()) : $related->the_post(); ?>
    				<div class="related-article four-col">
    					<figure class="entry-image">
    						<a href="<?php the_permalink(); ?>"><?php
    							if ( has_post_thumbnail() ) {
    								the_post_thumbnail( 'small');
    							} else { ?>
    								<img src="<?php  echo get_template_directory_uri(); ?>/images/default-image.png" alt="<?php the_title();  ?>" /><?php
    							} ?>
    						</a>
    					</figure>
    					<h5><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h5>
    				</div><?php
    			endwhile; ?>
    		</section><?php
    	}
    }
    wp_reset_postdata();

    Let me know how it goes, it should work fine.

    Thread Starter leeminhtoan

    (@leeminhtoan)

    Thank you, but it show : “Parse error: syntax error, unexpected ‘}’ in C:\xampp\htdocs\wp-content\themes\wiles 1.0.4\inc\related-posts.php on line 28”

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How can I change related post by category to related post by tags ?’ is closed to new replies.