WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Show posts with same tags as current single post (7 posts)

  1. carl-johan
    Member
    Posted 4 years ago #

    Hi

    I'm building a theme where I'd like to display thumbnails linking to posts that has the same tags as the post you are currently viewing underneath.

    Say I have two categories "Cars" and "Motorcycles", and a post tagged with "Red" and "Two-door". When viewing that single post I'd like to display thumbnails of each and every single post tagged either "Red" or "Two-door", wether they are in "Cars" or in "Motorcycles" or whatever.

    Found this script by MichealH on WP Recipes which is nearly what I want (great script though Micheal still!), but it only fetches posts that has the first tag not posts from every tag:

    <?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) {
    $first_tag = $tags[0]->term_id;
    $args=array(
    'tag__in' => array($first_tag),
    'post__not_in' => array($post->ID),
    'showposts'=>5,
    'caller_get_posts'=>1
    );
    $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() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
    <?php the_post_thumbnail(); ?>
    </a>
    <?php endwhile; }
    wp_reset_query();
    }
    ?>

    So, anyone know how to tweak that code to show posts from all tags not only the first?

    Then, secondary, if anyone know how to edit the same code to work outside the loop, I'd be very grateful!

    Thanks in advance!

  2. MichaelH
    Member
    Posted 4 years ago #

    <?php
    //for use in the loop, list all posts with tags contained on current post, exlcuding the current post
    $tags = wp_get_post_tags($post->ID);
    if ($tags) {
      $term_ids=array();
      foreach($tags as $tag) {
        array_push($term_ids,$term->term_id);
      }
      $args=array(
        'tag__in' => $term_ids,
        'post__not_in' => array($post->ID),
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'caller_get_posts'=> 1
      );
      $my_query = null;
      $my_query = new WP_Query($args);
      if( $my_query->have_posts() ) {
        echo 'List of Related Posts';
        while ($my_query->have_posts()) : $my_query->the_post(); ?>
          <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
    <?php the_post_thumbnail(); ?></a>
          <?php
        endwhile;
      }
    wp_reset_query();  // Restore global post data stomped by the_post().
    }
    ?>
  3. carl-johan
    Member
    Posted 4 years ago #

    Thanks a lot for getting back to me so quickly Micheal! Didn't got your code to work though, but I just finished one myself that does, and it's even working outside the loop;

    <?php global $post;
    $nextTagThumb='-1';
    $tags = wp_get_post_tags($post->ID);
    foreach ($tags as $tag) :
    ?>
    
    <?php
    if ($tags) {
    $what_tag = $tags[($nextTagThumb+'1')]->term_id;
    $args=array(
    'tag__in' => array($what_tag),
    'post__not_in' => array($post->ID),
    'showposts'=>100,
    'caller_get_posts'=>1
    );
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
    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_post_thumbnail(); ?>
    </a>
    </li>
    <?php endwhile;
    }
    wp_reset_query();
    $nextTagThumb = ($nextTagThumb+1);
    }
    ?>
    <?php endforeach; ?>

    As you will see, it's based on your original code, I just added a foreach loop for all the tags of the post and a variable that grows with each tagged post. Not sure how kosher my code is, and if you see an obvious error or issue please let me know, otherwise it's doing the job.

    Thanks again for getting back to me, was going to post the code I just finished when I saw your post.

    /Carl-Johan, Sweden

  4. MichaelH
    Member
    Posted 4 years ago #

    Looks good. Thanks for the feedback. Will mark this resolved.

  5. vinnyred
    Member
    Posted 3 years ago #

    @carl-johan and MichaelH - thanks for this. Most code only gets the first tag. This is perfect. Great work.

  6. vinnyred
    Member
    Posted 3 years ago #

    Ah. It displays posts with the same tag twice. Is there a way to get display a post only once in this list, given that it might a different tags?

  7. vinnyred
    Member
    Posted 3 years ago #

    I've sorted it sorry. I used the do not duplicate array coding from:
    http://codex.wordpress.org/The_Loop#Multiple_Loops

    set the array position 0 to be 1 and then go into the loop at the end of the lop set the current post id into the loop then check at the beginning of the loop if that post has already been inside this loop.

Topic Closed

This topic has been closed to new replies.

About this Topic