0

我想知道是否有办法让某个 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 消失。有任何想法吗?

4

1 回答 1

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 } ?>
于 2013-02-28T00:05:19.737 回答