2

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! :)

2
  • 1
    If I've understood your question correctly, this is what you're looking for pastebin.com/raw.php?i=DVChDpnC, look at how I added two else blocks to the code. I haven't tested for syntax errors.
    – soulseekah
    Oct 16, 2011 at 14:27
  • Actually it produces a syntax error "syntax error, unexpected '<'". I'll try and see if I can fix it. Thanks.
    – japanworm
    Oct 17, 2011 at 2:29

1 Answer 1

3

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="<?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
                    // 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.

7
  • First of all, thank you so much for taking time to help me out! Really appreciated. Your code works as in it's not showing anything if there are no related posts, however, there's one HUGE issue with the code you provided, so I think I need to further explain what else the code does. The container makes the posts scrollable and there are a "prev" and "next" button outside of the container so users can scroll through posts (thus the post 5-8 etc.). With your code the navigation arrows don't show up anymore. Any idea how to fix this? Thanks so much!
    – japanworm
    Oct 17, 2011 at 2:27
  • Also, it might help if you have a look at this question of mine as well: wordpress.stackexchange.com/questions/31229/… This also concerns the same code and explains a little bit better about the container and what it does (or is supposed to do).
    – japanworm
    Oct 17, 2011 at 2:32
  • Okay, so, pull that HTML markup back outside of the loop? I'll update my answer in the morning, with this change. Oct 17, 2011 at 2:36
  • But then the HTML markup will show up even if there are no related posts, right? That's why I suggested to just arrange it so that within the container a message will show up "Sorry, no related posts .." instead as I think that's easier to do? Anyways, thanks a lot. Will be awaiting your updated answer then :)
    – japanworm
    Oct 17, 2011 at 2:48
  • @japanworm I completely don't understand: either you do want HTML markup to appear outside of the loop, or else you don't want HTML markup to appear outside of the loop. If there are no posts, then what is there to scroll? Oct 17, 2011 at 3:05

Your Answer

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

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