我想有一个相关的框来显示-但只有当相关的帖子在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>
谢谢!公元
最合适的回答,由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())
}
}
?>