首页 > 解决方案 > 如果没有内容,则隐藏整个 div

问题描述

在我的网站中,我通过应用下面的代码来显示即将发生的事件,它只显示未来的事件。因此,每当有即将发生的事件时,它都会显示在我的主页上。

但是,当没有事件时,标题会Upcoming Events出现整个框。我试图在它为空时隐藏它,但由于我对 PHP 的了解,它不起作用,我在下面找不到解决方案。

我希望你们能在这方面帮助我

  <div class="service-event widget-event widget"> 
       <div clear="event-items clear">

         <div class="event"><h4 class="title">Upcoming Events</h4></div>

    <?php

    $custom_terms = wp_get_post_terms($post->ID, 'event-category');

    if( $custom_terms ){


      $tax_query = array();


      if( count( $custom_terms > 1 ) )
          $tax_query['relation'] = 'OR' ;


      foreach( $custom_terms as $custom_term ) {

          $tax_query[] = array(
              'taxonomy' => 'event-category',
              'field' => 'slug',
              'terms' => $custom_term->slug,
          );

      }


      $args = array( 'post_type' => 'event',
                      'posts_per_page' => 5,
                      'post_status'    => 'future',
                      'tax_query' => $tax_query );


      $loop = new WP_Query($args);

      if( $loop->have_posts() ) {

          while( $loop->have_posts() ) : $loop->the_post(); ?>

          <div class="item">

              <div class="date">
                  <div class="text">
                      <strong><?php echo get_the_date('j') ?></strong><br>
                      <?php echo get_the_date('M') ?><br>
                  </div>
              </div>

              <div class="info">
                  <h2 class="title">
                      <a href="<?php bloginfo('url') ?>/event/<?php echo $post->post_name ?>" class="underline"><?php the_title() ?></a>
                  </h2>

                  <?php
                  $cats = get_the_terms(get_the_ID(), 'event-country');
                  $names = array();

                  foreach ($cats as $cat) {
                      if ($cat->parent) {
                          array_push($names, "<strong>$cat->name</strong>");
                      }
                  }

                  foreach ($cats as $cat) {
                      if (!$cat->parent) {
                          array_push($names, $cat->name);
                      }
                  }
                  ?>
                  <div class="location">
                      <i class="icon icon-location-blue"></i>
                      <?php echo implode(', ', $names) ?>
                  </div>
              </div>

          </div>
          <?php

          endwhile;

      }

      wp_reset_query();

    }?>
    </div>
</div>

标签: phphtmlwordpress

解决方案


编辑您的代码如下,

<?php
    $custom_terms = wp_get_post_terms($post->ID, 'event-category');

    if ($custom_terms) {


        $tax_query = array();


        if (count($custom_terms > 1))
            $tax_query['relation'] = 'OR';


        foreach ($custom_terms as $custom_term) {

            $tax_query[] = array(
                'taxonomy' => 'event-category',
                'field' => 'slug',
                'terms' => $custom_term->slug,
            );
        }


        $args = array('post_type' => 'event',
            'posts_per_page' => 5,
            'post_status' => 'future',
            'tax_query' => $tax_query);


        $loop = new WP_Query($args);

        if ($loop->have_posts()) {
            ?> 
            <div class="service-event widget-event widget"> 
                <div clear="event-items clear"> 
                    <div class="event"><h4 class="title">Upcoming Events</h4></div>

                    <?php while ($loop->have_posts()) : $loop->the_post(); ?>

                        <div class="item">

                            <div class="date">
                                <div class="text">
                                    <strong><?php echo get_the_date('j') ?></strong><br>
                                    <?php echo get_the_date('M') ?><br>
                                </div>
                            </div>

                            <div class="info">
                                <h2 class="title">
                                    <a href="<?php bloginfo('url') ?>/event/<?php echo $post->post_name ?>" class="underline"><?php the_title() ?></a>
                                </h2>

                                <?php
                                $cats = get_the_terms(get_the_ID(), 'event-country');
                                $names = array();

                                foreach ($cats as $cat) {
                                    if ($cat->parent) {
                                        array_push($names, "<strong>$cat->name</strong>");
                                    }
                                }

                                foreach ($cats as $cat) {
                                    if (!$cat->parent) {
                                        array_push($names, $cat->name);
                                    }
                                }
                                ?>
                                <div class="location">
                                    <i class="icon icon-location-blue"></i>
                                    <?php echo implode(', ', $names) ?>
                                </div>
                            </div>

                        </div>
                        <?php
                    endwhile;
                    ?>
                </div>
            </div>  
            <?php
        }

        wp_reset_query();
    }
?>

希望这可以帮助。


推荐阅读