如果没有内容,让div消失

时间:2013-02-27 23:59:30

标签: php wordpress

我想知道是否有办法制作某个div显示:没有帖子时没有。

这是我到目前为止所提出的:

<div class="MVP-box">

    <?php 
        $loop = new WP_Query(array('post_type' => 'MVP', 'posts_per_page' => 1)); 
    ?>
    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    <?php   
        $custom = get_post_custom($post->ID);
        $screenshot_url = $custom["screenshot_url"][0];
        $website_url = $custom["website_url"][0];
    ?>


        <div class="post-entry">
        <div class="MVP-title">
        <?php the_title(); ?>
        </div>
        <div class="MVP-thumbnail">
            <?php the_post_thumbnail('MVP-picture'); ?>
        </div>
       <?php the_content(); ?>
        </div>
        <?php endwhile; ?>  

</div>

我想知道的是,当没有帖子内容时,是否有办法让MVP-box div消失。有什么想法吗?

1 个答案:

答案 0 :(得分:3)

你可以在绘制div之前检查一下have_posts吗?

<?php 
  $loop = new WP_Query(array('post_type' => 'MVP', 'posts_per_page' => 1)); 
  if ($loop->have_posts()) { ?>
   <div class="MVP-box">


<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php   
    $custom = get_post_custom($post->ID);
    $screenshot_url = $custom["screenshot_url"][0];
    $website_url = $custom["website_url"][0];
?>


    <div class="post-entry">
    <div class="MVP-title">
    <?php the_title(); ?>
    </div>
    <div class="MVP-thumbnail">
        <?php the_post_thumbnail('MVP-picture'); ?>
    </div>
   <?php the_content(); ?>
    </div>
    <?php endwhile; ?>  

</div>
<?php } ?>