• Dave

    (@go3asy)


    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)
  • Michael

    (@alchymyth)

    move this code <?php _e('Related News', 'minti'); ?> to after the if of the main loop;
    example:

    <?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  $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() ) {
    					   _e('Related News', 'minti');
    					    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_postdata();
    					  }
    					}
    					?>
    
    			<?php } ?>
    Thread Starter Dave

    (@go3asy)

    Thanks, made a few ammendments to display as a heading. Had to close and re open the PHP tags but its working a treat ??

    <?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) {
    					?>
    					  <ul>
    					<?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() ) { ?>
                                              <h3 class="title"><span>Related News</span></h3>
                                              <?php	while ($my_query->have_posts()) : $my_query->the_post(); ?>
    					      <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?> <span>(<?php the_time(get_option('date_format')); ?>)</span></a></li>
    					      <?php
    					    endwhile;
    					    wp_reset_query();
    					  }
    					}
    					?>
    					 </ul>
    			</div>
    			<?php } ?>
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.