Skip to content

Instantly share code, notes, and snippets.

@mkdizajn
Created May 15, 2013 12:41
Show Gist options
  • Save mkdizajn/5583716 to your computer and use it in GitHub Desktop.
Save mkdizajn/5583716 to your computer and use it in GitHub Desktop.
wordpress get related tags
<?php
// Related Posts Function (call using bones_related_posts(); )
function bones_related_posts() {
echo '<ul id="bones-related-posts">';
global $post;
$tags = wp_get_post_tags($post->ID);
if($tags) {
foreach($tags as $tag) { $tag_arr .= $tag->slug . ','; }
$args = array(
'tag' => $tag_arr,
'numberposts' => 5, /* you can change this to show more */
'post__not_in' => array($post->ID)
);
$related_posts = get_posts($args);
if($related_posts) {
foreach ($related_posts as $post) : setup_postdata($post); ?>
<li class="related_post"><a class="entry-unrelated" href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; }
else { ?>
<?php echo '<li class="no_related_post">' . __( 'No Related Posts Yet!', 'bonestheme' ) . '</li>'; ?>
<?php }
}
wp_reset_query();
echo '</ul>';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment