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.

I want to have a related box to show up - but only if a related post is inside the div. If theres no post the whole "related_box" div should be not shown. Any idea?

Here's the code so far.

    <div id="related_box" class="related">
    <div class="movie_header">
    <div id="featured"><h2>Related</h2>

    </div>
    <?php
    global $post;
    $terms = get_the_terms( $post->ID , 'movies', 'string');
    $do_not_duplicate[] = $post->ID;

    if(!empty($terms)){
        foreach ($terms as $term) {
            query_posts( array(
            'movies' => $term->slug,
            'showposts' => 4,
            'caller_get_posts' => 1,
            'post__not_in' => $do_not_duplicate ) );
            if(have_posts()){
                while ( have_posts() ) : the_post(); $do_not_duplicate[] = $post->ID; ?>
    <div class="related">
    <div class="inner">
    <div class="thumb"><?php if ( has_post_thumbnail()) the_post_thumbnail('movie-thumb'); ?></div>
    <h2 class="description">
   text</h2>
    <a href="<?php the_permalink(); ?>"><h1 class="title" ><?php the_title(); ?> </h1></a>


    </div>
    </div>
                <?php endwhile; wp_reset_query();
            }
        }
    }
    ?>

    </div>


    </div>

thanks! AD

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

move the div section from the start to after the if(have_posts()) { line, and move the corresponding closing divs to just before the closing bracket } of this if statement;

full code:

<?php     
global $post;     
    $terms = get_the_terms( $post->ID , 'movies', 'string');     
    $do_not_duplicate[] = $post->ID;      
    if(!empty($terms)){         
        foreach ($terms as $term) {             
        query_posts( array(             
            'movies' => $term->slug,             
            'showposts' => 4,             
            'caller_get_posts' => 1,             
            'post__not_in' => $do_not_duplicate 
            ) );    

            if(have_posts()){ ?>
        <div id="related_box" class="related">     
        <div class="movie_header">     
        <div id="featured"><h2>Related</h2>      
        </div>          
            <?php while ( have_posts() ) : the_post(); 
            $do_not_duplicate[] = $post->ID; ?>     
            <div class="related">     
                <div class="inner">     
                    <div class="thumb"><?php if ( has_post_thumbnail()) the_post_thumbnail('movie-thumb'); ?></div>     
                <h2 class="description">text</h2>     
                <a href="<?php the_permalink(); ?>"><h1 class="title" ><?php the_title(); ?> </h1></a>   
                </div>     
            </div>                 
            <?php endwhile; wp_reset_query(); ?>        
        </div>       
        </div>          
            <?php } //ends: if(have_posts())
        }     
    }     
?>    
share|improve this answer
    
thank you Michael- it worked. –  ad2003 Jul 14 '12 at 18:01
add comment

It might sound odd ... but you are so close

in fact a standard single.php template usually has a loop like this. Basically you put your <div>related posts</div> as part of your wp_query loop.

you should start with something like

<?php new wp_query
if has_post {
echo '<div>';
}

else{
// do nothing
}
share|improve this answer
    
Hi Damien thanks for the reply but it doesn't work for me... –  ad2003 Jul 14 '12 at 12:00
add comment

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.