WordPress.org

Ready to get started?Download WordPress

Forums

Related post hardcode (3 posts)

  1. Ninetto
    Member
    Posted 4 years ago #

    Hi!
    I am using the code to get the related post using tags.
    How can i show this if no tags are found <p>No tags Found</p>

    <?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) {
      echo 'Related Posts';
      $first_tag = $tags[0]->term_id;
      $args=array(
        'tag__in' => array($first_tag),
        'post__not_in' => array($post->ID),
        'showposts'=>2,
        'caller_get_posts'=>1
       );
      $my_query = new WP_Query($args);
      if( $my_query->have_posts() ) {
        while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <div class="related-posts"><div class="Small-Landscape-image">
    <a href="<?php the_permalink() ?>" rel="bookmark">
    <?php $image = get_post_meta($post->ID, 'popular_image', true); /* Replace lead_image with the name of the custom field for the image you want to use */ ?>
    <img style=" margin:0px; border:0px;" width="70" height="54" alt="<?php the_title(); ?>" src="<?php echo $image ?>" /></a></div>
    
          <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a><br />
    <?php if ($post->post_excerpt != "" ) {the_excerpt();
    }
    else {the_content_rss('', FALSE, '', 25);
    }
    ?></p>
    </div>
          <?php
        endwhile;
      }
    }
    ?>
  2. esmi
    Forum Moderator
    Posted 4 years ago #

    You could try changing the last few lines from:

    </div>
          <?php
        endwhile;
      }
    }
    ?>

    to

    </div>
    <?php else :?>
    <div class="related-posts"><p>No tags Found</p></div>
    <?php endif;?>
    <?php
        endwhile;
      }
    }
    ?>
  3. Ninetto
    Member
    Posted 4 years ago #

    Thanks but it didn't work.
    What i am looking for is when viewing a post that has a tag the related posts show which the code above does, but when there are no related post it should show the message
    <p>No tags found<p>.

Topic Closed

This topic has been closed to new replies.

About this Topic