仅当里面有相关帖子时才显示div

时间:2012-07-14 作者:ad2003

我想有一个相关的框来显示-但只有当相关的帖子在div内时。如果没有帖子,则不应显示整个“related\\u box”div。有什么想法吗?

这是目前为止的代码。

    <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>
谢谢!公元

2 个回复
最合适的回答,由SO网友:Michael 整理而成

将div部分从开始移动到if(have_posts()) { 行,并将相应的结束div移到结束括号的正前方} 本if声明;

完整代码:

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

SO网友:Damien

听起来可能很奇怪。。。但你离得太近了

实际上是标准单曲。php模板通常有这样一个循环。基本上你把你的<div>related posts</div> 作为wp\\U查询循环的一部分。

你应该从以下内容开始

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

else{
// do nothing
}

结束

相关推荐

确定是否有比`Query_Posts()`中要求的更多的帖子?

我正在使用自定义query_posts() 作用我限制了showposts 参数设置为25,那么如果仍有产品(帖子)要加载,如何知道激活分页?我的query_posts() 代码如下所示:$args = array( \'post_type\' => \'product\', \'taxonomy\' => \'product_cat\', \'term\' => $idObj->slug, // category slug