Question

In a single post template I am trying to output some header text in a <div> and display links to related posts in a <ul> if related posts exist (based on tags). If there are no posts, nothing should be displayed. $idspost[] = $post->ID gets the posts which should be displayed. The loop is one of a series of loops and the retrived post ID's are also passed on to the next loop in order to avoid duplicate posts showing up. All works well so far. I have echoed out the ID's to test, but with my basic skills I don't realize how I will display the <div>,<ul> and retrieve the post ID's in the standard way:

<?php if (have_posts()) : ?>
  <div>
    <?php while (have_posts()) : the_post(); ?>
      … //get the permalink, title etc...
    <?php endwhile; ?> 
  </div>
<?php endif; ?>

The post ID's are retrieved:

<?php
    $tags = wp_get_post_tags($post->ID);
    if ($tags) {
      $second_tag = $tags[1]->term_id;
      $args=array(
        'tag__in' => array($second_tag),
        'post__not_in' => array($post->ID),
        'showposts'=>5, //Display this number of related posts
        'ignore_sticky_posts'=>1
       );
      $my_query = new WP_Query($args);
      if( $my_query->have_posts() ) {
          while ($my_query->have_posts()) : $my_query->the_post(); 
          if (!in_array($post->ID, $ids)) 
          {; $idspost[] = $post->ID; ?>

    <?php }
        $ids[]= $post->ID; 
        $idspost[] = $post->ID;
        endwhile;
      }
    }
    ?>

Would be very grateful for help!

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top