I’ve been trying for a while now to make it so that there is nothing shown if there are no Related Posts.
Currently the headline and the container (of course empty) are displayed.
Like I said I tried a lot and I came to the conclusion that it might be too difficult if not impossible to get rid of everything as the code is quite complicated:
<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">
<!-- post 1-4 -->
<div>
<?php
$backup = $post;
$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'=>4, // Number of related posts that will be shown.
'caller_get_posts'=>1
);
$my_query = new wp_query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) {
$my_query->the_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
}
echo '';
}
}
$post = $backup;
wp_reset_query();
?>
</div>
<!-- post 5-8 -->
<div>
......
</div>
</div>
</div>
<!-- "next page" action -->
<a class="next browse right"></a>
Instead, I want to leave the headline and container, but make it so that the container is not empty.
For that I want to use an “if” or “elseif” tag
Something like that:
<!-- If there are no related posts:-->
<p class="noposts">Sorry, but there are no related posts for this particular entry.</p>
I just cannot seem to figure out how to implement it correctly.
It would be great if somebody with a bit knowledge of conditional tags could help me figure it out.
Thanks a lot! 🙂
I don’t see why it would be impossible to do what you want to do. I think you just need to do a bit of rearranging. Put your custom query code first, then put your containing HTML markup inside the
if ( $my_query->have_posts() )
, then put your related-post markup inside thewhile ( $my_query->have_posts() )
, then put your no-related-post markup inside anelse {}
statement, then put your closing-containing HTML markup outside theelse {}
statement, then close yourwhile
statement:Note: I removed the “Post 5-8” divs, as I wasn’t sure how they were supposed to fit into this markup.