Problem with “conditional tag”: if empty

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 the while ( $my_query->have_posts() ), then put your no-related-post markup inside an else {} statement, then put your closing-containing HTML markup outside the else {} statement, then close your while statement:

<?php
// First, backup the default $postdata
$backup = $post;

// Now, override the default
$tags = wp_get_post_tags($post->ID);

// Now, open the if ( $tags ) statement
if ($tags) {
    $tag_ids = array();
    foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;

    // Now, setup your custom query args                    
    $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
    );
    // Now, perform your custom query
    $my_query = new wp_query($args);

    // Next, open your custom query IF statement
    if( $my_query->have_posts() ) {
        // We have posts, so let's output the opening-containing markup
        ?>

        <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 
                // Now, open your custom query WHILE statement
                while ($my_query->have_posts()) {                                                               
                    $my_query->the_post();
                    ?>

                    <div class="relatedPosts"><a href="https://wordpress.stackexchange.com/questions/31242/<?php the_permalink() ?>" rel="bookmark"><?php the_post_thumbnail(array(120,80)); ?></a>
                    <div class="relatedPosts_title"><a href="https://wordpress.stackexchange.com/questions/31242/<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></div></div>

                    <?php
                    // Now, close the while $my_query->have_posts() statement
                } 
                // I'm not sure why this is here?
                echo '';
                // Now, output your closing-containing HTML content
                ?>    
                </div>

            </div>

        </div>

        <!-- "next page" action -->
        <a class="next browse right"></a>
        <?php
    // Now, close the if $my_query->have_posts() statement
    // and open the ELSE statement, for your no-posts content
    } else {
        ?>
        <!-- If there are no related posts:-->
        <p class="noposts">Sorry, but there are no related posts for this particular entry.</p>
        <?php
    // Now, close the ELSE statement
    }
// Now, close the if ( $tags ) statement
}
// Now, reset the default query
$post = $backup;
wp_reset_query();
?>

Note: I removed the “Post 5-8” divs, as I wasn’t sure how they were supposed to fit into this markup.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)