How to get ralated posts in WordPress without Plugin

This tutorial will help you get related posts in WordPress using custom function in your WordPress theme without plugin. If no related found this function will auto show lastest posts to single content.

Display Related Posts in WordPress without a Plugin

Step 1: Open functions.php in your WordPress theme add code bellow

/*-----------------------------------------------------------------------------------*/
/*	Related Posts
 /*-----------------------------------------------------------------------------------*/
function hr_related_posts() {
	global $post, $wpdb;
	$backup = $post;  // backup the current object
	$tags = wp_get_post_tags($post->ID);
	$tagIDs = array();
	if ($tags) {
		$tagcount = count($tags);
		for ($i = 0; $i < $tagcount; $i++) {
			$tagIDs[$i] = $tags[$i]->term_id;
		}

		$args=array(
				'tag__in' => $tagIDs,
				'post__not_in' => array($post->ID),
				'showposts'=>8,
				'caller_get_posts'=>1,
		);
		$my_query = new WP_Query($args);
		  if( $my_query->have_posts() ) { $related_post_found = true; ?>
			<h3 class="section-title">Related Posts</h3>
				<div class="related-loop row">
		    <?php $post_count = 1; while ($my_query->have_posts()) : $my_query->the_post(); ?>
		    		<div class="col-lg-3 col-md-3 col-xs-3 col-sm-6">
					 <div <?php echo 'class="hentry product-block"';?> itemscope itemtype="http://schema.org/Article">
				 		<a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_post_thumbnail('medium', array('class' => 'img-responsive','itemprop'=>'image')); ?></a>
						<h2 itemprop="name" class="entry-title"><a title="<?php the_title(); ?>" href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
						<span class="entry-meta"><i class="fa fa-calendar"></i> <?php the_time(get_option('date_format')); ?></span>
						<meta itemprop="url" content="<?php the_permalink(); ?>"/>

	                 </div>
	               	</div>
		    <?php $post_count++; endwhile; wp_reset_query(); ?>
				</div>
		  <?php }
		}

		//show recent posts if no related found
		if(!$related_post_found){ ?>
			<h3 class="section-title">Lastest Posts</h3>
			<div class="related-loop row">
			<?php
			$post_count = 1;
			$posts = get_posts('numberposts=8&offset=0');
			foreach($posts as $post) { ?>
				<div class="col-lg-3 col-md-3 col-xs-3 col-sm-6">
					<div <?php echo 'class="hentry product-block"';?> itemscope itemtype="http://schema.org/Article">
					<a href="<?php the_permalink(); ?>" rel="bookmark" ><?php the_post_thumbnail('medium', array('class' => 'img-responsive','itemprop' => 'image')); ?></a>
					<h2 class="entry-title" itemprop="name"><a title="<?php the_title(); ?>" href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
					<span class="entry-meta"><i class="fa fa-calendar"></i> <?php the_time(get_option('date_format')); ?></span>
					<meta itemprop="url" content="<?php the_permalink(); ?>"/>

					</div>
				</div>
			<?php $post_count++; } wp_reset_query(); ?>
			</div>
			<?php
		}
		wp_reset_query();
	}

Step 2: Open Single.php file, insert this code to position where you want show related posts

<?php hr_related_posts()?>
Share this :

Comments are closed.

Related post

Free Responsive Website templates

ThemeVina is Best site for free sharing and download Wordpress themes, Joomla templates, Responsive website templates and more HTML templates.

All template in Site are free. You can download and install for your site.

Contact us

  • Brand: Theme Vina
  • Email: admin@themevina.com
  • Website: www.themevina.com
  • Address: 367 Tran Hung Dao Street
  • Location: Thai Binh City, Thai Binh, Viet Nam

Newsletter