Skip to content

Instantly share code, notes, and snippets.

@aktaruzzamancse
Created December 22, 2016 05:57
Show Gist options
  • Save aktaruzzamancse/1a07872f406d68761425172ec70f72a9 to your computer and use it in GitHub Desktop.
Save aktaruzzamancse/1a07872f406d68761425172ec70f72a9 to your computer and use it in GitHub Desktop.
Wp related posts by tag
<?php
/**
* MODULE SECTION: related posts
*
* The template used for displaying related posts
*
* @version: 1.2
*
* NOT WORKING
*/
?>
<div class="related-posts">
<?php
global $post;
$tags = get_the_tags($post->ID);
$posttyp = $post->post_type;
//echo $posttyp;
if ($tags) {
$tag_slugs = array();
foreach($tags as $tag) :
$tag_slugs [] = $tag->slug;
endforeach;
$args = array('posts_per_page'=>2, 'tag' => $tag_slugs,'post_type' => $posttyp,'post__not_in' => array($post->ID));
$the_query = new WP_Query( $args );?>
<?php if( $the_query->have_posts() ): ?>
<h4>Related Posts</h4>
<ul>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_postdata();
?>
<?php } ?>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment