0

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

1

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())
        }     
    }     
?>    
0
0

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
}
1
  • Hi Damien thanks for the reply but it doesn't work for me...
    – ad2003
    Jul 14 '12 at 12:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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