Only show related posts if there is any, not just if there are tags
-
Basically in my template I have a piece of code which shows the ehading related posts based on if there is any tags in the post. Some posts have tags but no matching/rleated ones so the heading shows with nothing underneath and this is un tidy.
Trying to figure out a clean way of doing this and getting nowhere, basically want the whole lot hidden unless there are matching posts in the tags…here is the code:
<?php //for use in the loop, list 5 post titles related to first tag on current post $tags = wp_get_post_tags($post->ID); if ($tags) { ?> <?php _e('Related News', 'minti'); ?> <?php $first_tag = $tags[0]->term_id; $args=array( 'tag__in' => array($first_tag), 'post__not_in' => array($post->ID), 'showposts'=>3 ); $my_query = new WP_Query($args); if( $my_query->have_posts() ) { while ($my_query->have_posts()) : $my_query->the_post(); ?> <a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?> (<?php the_time(get_option('date_format')); ?>)</a> <?php endwhile; wp_reset_query(); } } ?> <?php } ?>
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘Only show related posts if there is any, not just if there are tags’ is closed to new replies.