Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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">

<?php

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

 $args=array(
'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); ?>



<?php
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>

<?php

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

<?php
endwhile; endif;
?>



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

 </div>

<!-- "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.

share|improve this question
    
virtually duplicate: wordpress.stackexchange.com/questions/31242/… –  Michael Oct 20 '11 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 Oct 20 '11 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 Oct 20 '11 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 Oct 21 '11 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 Oct 21 '11 at 14:04

1 Answer 1

up vote 1 down vote accepted

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:

<?php 
endwhile; endif; 
?>

to:

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

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

edit:

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) ?>   

to:

<?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) 
?>  
share|improve this answer
    
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 Oct 21 '11 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 Oct 21 '11 at 8:20
    
Awesome, thank you so much! –  japanworm Oct 21 '11 at 8:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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