0

I am using thesis and i am trying to display the related post by there tag name.

suppose i have post and that post have three tag or multiple

tag like tag:- 1) car, 2)automobile, 3) mechanical

so i want display the related post by the tag name of car,automobile,mechanical.

I am trying the bellow code with the thesis hook but its not displaying the related post by the tag name of that post its displaying another related post which have don't have any connection with these three tags of this post.

so any body can tell hot do this.

my function code:-

function tagpost(){
        global $post;
        $tag = get_the_tags();
        foreach ($tag as $tags){
                 ?>
       <div class="relatedpostAdd post<?php echo $tags->term_id; ?>">
        <h3 style="margin:5px 0 "><?php echo $tags->name; ?></h3>
    <ul>
<?php
        $posts = get_posts('numberposts=6&term_id='. $tags->term_id);
        foreach($posts as $post) {
        ?>
    <li>
    <div class="images_releted">
        <a class="post_thumbnail" href="<? the_permalink()?>" rel="bookmark" 
          title="<?php the_title(); ?>">
        <?php echo the_post_thumbnail('thumbnail') ?>
        </a>
    </div>
        <a class="post_thumbnail_link" href="<? the_permalink();?>" rel="bookmark" 
         title="<?php the_title(); ?>">
        <?php the_title(); ?></a>               
    </li>

<?php } ?>
    </ul>
  </div>
<?php } 
}
add_action('thesis_hook_after_post_box_related_posts','tagpost');
3
  • Your get_posts arguments are wrong. Use tag_id, not term_id Commented Nov 19, 2014 at 7:49
  • see i have chaged it to 'get_posts('numberposts=6&term='. $tags->tag_id);' but same issue post not pointing to related 'tag' shall i use tag instead of 'term' in 'ger_posts' argument Commented Nov 19, 2014 at 7:58
  • @PieterGoosen i have changed it displaying wrong related post Commented Nov 19, 2014 at 8:09

1 Answer 1

2

As stated in a comment, your get_posts parameters are wrong. There is no such parameter as term_id. You should make use tag which uses the tag slugs as a string or tag__in which uses the tag ids in an array

This will work

$posts = get_posts('numberposts=6&tag='. $tags->slug);

or this will work

$posts = get_posts('numberposts=6&tag__in=' . array( $tags->term_id ));

There are some other issues as well that I want to highlight here

  • You should set the post ID in get_the_tags(). Do something like this get_the_tags( $post->ID );

  • Set the plural name as the variable name which will hold your array from your function and use the single name as your value in your foreach loop. This eliminates confusion and is easier to debug. If you read this, $tags->name, you immediately think of more than one tag, yet this is used to retrieve the specific tag's name as $tags are actually your value. You should do something like this which IMO is far better to understand and less confusing

    $tags = get_the_tags();
    foreach ( $tags as $tag ){
        echo $tag->name;
    }
    

EDIT

To exclude the current post, you can do the following

$posts = get_posts('numberposts=6&tag__in=' . array( $tags->term_id ) . '&post__not_in=' . array( $post->ID) );
7
  • great answer its working great but have one issue is this possible to hide related post which is display as main post Commented Nov 20, 2014 at 5:33
  • you are getting me right but i am confusing where to place this code in get post Commented Nov 20, 2014 at 5:51
  • See my update. Added comment to answer Commented Nov 20, 2014 at 6:00
  • ok fine i get it Commented Nov 20, 2014 at 6:05
  • i was try this updated code but its not displaying related post it displaying only tag heading of current post Commented Nov 20, 2014 at 6:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.