For my related posts I have set up a scrollable container. I need the HTML to be outside of the loop or otherwise the layout will be a mess if there are no related posts to display (missing divs that are inside the loop only if there are related posts to be shown).

So, the only option I have is to display a message within the container that only shows up if there are no related posts to display. Something like: "sorry but there are no related posts for this entry"

Unfortunately I'm not sure how to modify the code / how to implement it into the current code:

<h2>Related Posts</h2>                      

<!-- "previous page" action -->
<a class="prev browse left"></a>

<!-- root element for scrollable -->
<div class="scrollable" id=chained>  

    <!-- root element for the items -->
    <div class="items">


 $tags = wp_get_post_tags($post->ID);
 if ($tags) {
     $tag_ids = array();
     foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;

'tag__in' => $tag_ids,
'post__not_in' => array($post->ID),
'showposts'=>12, // get all the posts at once, then split them up afterwards.
'ignore_sticky_posts' => 1

$posts_per_block = 4;

$my_query = new wp_query($args); ?>

if( $my_query->have_posts() ) : while ($my_query->have_posts()) : $my_query->the_post();
if ( ($my_query->current_post) == 0 ) {
    echo '<div>';
elseif ( ( $my_query->current_post ) % $posts_per_block == 0 ) {
    echo '</div><div>'; //close and open a new div every nth post

<div class="relatedPosts"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_post_thumbnail(array(120,80)); ?></a>
<div class="relatedPosts_title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></div></div>


if ( ( $my_query->current_post + 1) == ( $my_query->post_count ) ) {
    echo '</div>'; //close the block if you run out of posts
} ?>

endwhile; endif;

<?php } // end if($tags) ?>


<!-- "next page" action -->
<a class="next browse right"></a>

Here's an example page without any tags.

Here's an example with tags and related posts.

Thanks so much in advance to anybody who can help me fix this.

  • virtually duplicate: wordpress.stackexchange.com/questions/31242/…
    – Michael
    Commented Oct 20, 2011 at 14:33
  • @Michael: Not really. The code has changed quite a bit since then - and so did the problem that comes with the whole code. I thought this time instead of editing my old post it might be better to create a new questions, but it seems that wasn't such a good idea. Sorry about that!
    – japanworm
    Commented Oct 20, 2011 at 14:53
  • I agree with Michael. You're still trying to solve the same problem and haven't accepted an answer yet on the previous question. If the previous issue were substantially different and you'd already accepted an answer and moved on I might see your argument ... but since they're not really different and you haven't yet, it would be a better idea to modify the original question with new details so you'll get a good answer. I personally vote to close/merge with the original question, but will wait for you to respond before I do.
    – EAMann
    Commented Oct 20, 2011 at 16:27
  • @EAMann: I wouldn't mind merging the two questions, then, but how would I do that? Can I even? I've never done that before. The reason why I haven't accepted an answer in the previous question entry yet is because I only accept questions that actually solved my problem. People often tend to just post an answer and never come back to check if their answer actually worked or not. Thanks a lot for your suggestion :)
    – japanworm
    Commented Oct 21, 2011 at 2:26
  • Seeing as you accepted an answer below, I won't merge them this time. If you ever need to merge duplicate or similar questions, just ask a moderator and we'll take care of it.
    – EAMann
    Commented Oct 21, 2011 at 14:04

1 Answer 1


I still can't see how the code and the problem is substantially different from your original topic;

however, try to add your text into the typical 'else' location for a loop if no posts are found; change this line:

endwhile; endif; 


else :
echo '<h3 class="no-related-posts">sorry but there are no related posts for this entry</h3>';

the css class is provided to make selective styling of the text easier.


the above is for the case if the post does have tags, but if there are no related posts with the same tag. it needs to stay in place.

in addition, there should be a message, when there are no tags; change this line:

<?php } // end if($tags) ?>   


<?php } else { //if no tags, show message
echo '<h3 class="no-related-posts">sorry but there are no related posts for this entry</h3>';
} // end if($tags) 
  • Thank you very much for helping me although I caused trouble. I modified the code like you suggested, however the message is not showing up (please refer to the link in my question where it says "post without tags"). Any idea why? Thanks.
    – japanworm
    Commented Oct 21, 2011 at 2:24
  • 1
    @japanworm - I see that i missed that the whole section is wrapped into a if ($tags) conditional; i.e. if the post does not have tags, then the query loop is not execued at all. I'll amend my answer.
    – Michael
    Commented Oct 21, 2011 at 8:20

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.