最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Hiding a row in the loop if empty

programmeradmin2浏览0评论

I'm trying to hide posts that have a certain category, but when a post is hidden, it's displaying and empty row. I've tried to move around my conditional statements but it just breaks my logic or page structure.

How do I hide a row ( <div class"row episodes-feed-wrap"> ) in this loop if the category is 'daily-email'?

Here is my code:

<div class="col-md-9" style="padding:0;">
      <div class="episodes-feed">
        <div class="container-fluid">
                    <?php $catquery = new WP_Query(array(
              'orderby' => 'date',
              'order' => 'DESC'
          )); ?>

          <?php while($catquery->have_posts()) : $catquery->the_post(); ?>
              <?php $backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'large' );?>

              <?php
                  $post_term_objects = wp_get_post_terms( $post->ID, 'category' );
                  $post_terms = [];
                  foreach ( $post_term_objects as $post_term_object ) {
                      $post_terms[] = $post_term_object->slug;
                  }
              ?>
              <div class="row episodes-feed-wrap">
                  <?php if ( ! in_array( 'stories', $post_terms, false) &! in_array('blog', $post_terms, false) &! in_array('daily-email', $post_terms, false)) : ?>
                      <div class="col-4 episodes-feed-thumb">
                          <a href="<?php the_permalink() ?>" rel="bookmark">
                              <?php echo get_the_post_thumbnail( $post->ID, 'large' ); ?>
                          </a>
                      </div>
                      <div class="col-8 featured-article">
                          <div class="container" style="padding:0;">
                              <h4 class="episodes-feed-cat">
                                  <?php $categories = get_the_category();
                                  if ( ! empty( $categories ) ) {
                                      echo '<a href="' . esc_url( get_category_link( $categories[0]->term_id ) ) . '">' . esc_html( $categories[0]->name ) . '</a>';
                                  }?>
                              </h4>
                              <a href="<?php the_permalink() ?>" rel="bookmark">
                                  <h2 class="episodes-title">
                                      <?php the_title(); ?>
                                  </h2>
                              </a>
                              <div class="episodes-excerpt">
                                  <a href="<?php the_permalink() ?>" rel="bookmark">
                                      <?php echo excerpt(25); ?>
                                  </a>
                              </div>
                              <div class="episodes-feed-info-wrap">
                                  <div class="episodes-feed-author">
                                      <a href="<?php the_permalink() ?>" rel="bookmark">
                                          <?php the_author(); ?>  &#8226; <?php echo meks_time_ago(); /* post date in time ago format */ ?>
                                      </a>
                                  </div>
                              </div>

                          </div><!-- end container -->
                      </div>
                  <?php elseif(! in_array('daily-email', $post_terms, false)) : ?>

                      <div class="col-4 episodes-feed-thumb">
                          <a href="<?php the_permalink() ?>" rel="bookmark">
                              <?php echo get_the_post_thumbnail( $post->ID, 'large' ); ?>
                          </a>
                      </div>

                      <div class="col-8 featured-article">
                          <div class="container" style="padding:0;">
                              <h4 class="episodes-feed-cat">
                                  <?php $categories = get_the_category();
                                  if ( ! empty( $categories ) ) {
                                      echo '<a href="' . esc_url( get_category_link( $categories[0]->term_id ) ) . '">' . esc_html( $categories[0]->name ) . '</a>';
                                  }?>
                              </h4>
                              <a href="<?php the_permalink() ?>" rel="bookmark">
                                  <h2 class="episodes-title">
                                      <?php the_title(); ?>
                                  </h2>
                              </a>
                              <div class="episodes-excerpt">
                                  <a href="<?php the_permalink() ?>" rel="bookmark">
                                      <?php echo excerpt(25); ?>
                                  </a>
                              </div>
                              <div class="episodes-feed-info-wrap">
                                  <div class="episodes-feed-author">
                                      <a href="<?php the_permalink() ?>" rel="bookmark">
                                        bloggy and storrieies
                                          <?php the_author(); ?>  &#8226; <?php echo meks_time_ago(); /* post date in time ago format */ ?>
                                      </a>
                                  </div>
                              </div>

                          </div><!-- end container -->
                      </div>

                    <?php endif; ?>
              </div>
          <?php endwhile;
          wp_reset_postdata();
          ?>
        </div>
      </div>
    </div><!-- end col 9 --> 

I'm trying to hide posts that have a certain category, but when a post is hidden, it's displaying and empty row. I've tried to move around my conditional statements but it just breaks my logic or page structure.

How do I hide a row ( <div class"row episodes-feed-wrap"> ) in this loop if the category is 'daily-email'?

Here is my code:

<div class="col-md-9" style="padding:0;">
      <div class="episodes-feed">
        <div class="container-fluid">
                    <?php $catquery = new WP_Query(array(
              'orderby' => 'date',
              'order' => 'DESC'
          )); ?>

          <?php while($catquery->have_posts()) : $catquery->the_post(); ?>
              <?php $backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'large' );?>

              <?php
                  $post_term_objects = wp_get_post_terms( $post->ID, 'category' );
                  $post_terms = [];
                  foreach ( $post_term_objects as $post_term_object ) {
                      $post_terms[] = $post_term_object->slug;
                  }
              ?>
              <div class="row episodes-feed-wrap">
                  <?php if ( ! in_array( 'stories', $post_terms, false) &! in_array('blog', $post_terms, false) &! in_array('daily-email', $post_terms, false)) : ?>
                      <div class="col-4 episodes-feed-thumb">
                          <a href="<?php the_permalink() ?>" rel="bookmark">
                              <?php echo get_the_post_thumbnail( $post->ID, 'large' ); ?>
                          </a>
                      </div>
                      <div class="col-8 featured-article">
                          <div class="container" style="padding:0;">
                              <h4 class="episodes-feed-cat">
                                  <?php $categories = get_the_category();
                                  if ( ! empty( $categories ) ) {
                                      echo '<a href="' . esc_url( get_category_link( $categories[0]->term_id ) ) . '">' . esc_html( $categories[0]->name ) . '</a>';
                                  }?>
                              </h4>
                              <a href="<?php the_permalink() ?>" rel="bookmark">
                                  <h2 class="episodes-title">
                                      <?php the_title(); ?>
                                  </h2>
                              </a>
                              <div class="episodes-excerpt">
                                  <a href="<?php the_permalink() ?>" rel="bookmark">
                                      <?php echo excerpt(25); ?>
                                  </a>
                              </div>
                              <div class="episodes-feed-info-wrap">
                                  <div class="episodes-feed-author">
                                      <a href="<?php the_permalink() ?>" rel="bookmark">
                                          <?php the_author(); ?>  &#8226; <?php echo meks_time_ago(); /* post date in time ago format */ ?>
                                      </a>
                                  </div>
                              </div>

                          </div><!-- end container -->
                      </div>
                  <?php elseif(! in_array('daily-email', $post_terms, false)) : ?>

                      <div class="col-4 episodes-feed-thumb">
                          <a href="<?php the_permalink() ?>" rel="bookmark">
                              <?php echo get_the_post_thumbnail( $post->ID, 'large' ); ?>
                          </a>
                      </div>

                      <div class="col-8 featured-article">
                          <div class="container" style="padding:0;">
                              <h4 class="episodes-feed-cat">
                                  <?php $categories = get_the_category();
                                  if ( ! empty( $categories ) ) {
                                      echo '<a href="' . esc_url( get_category_link( $categories[0]->term_id ) ) . '">' . esc_html( $categories[0]->name ) . '</a>';
                                  }?>
                              </h4>
                              <a href="<?php the_permalink() ?>" rel="bookmark">
                                  <h2 class="episodes-title">
                                      <?php the_title(); ?>
                                  </h2>
                              </a>
                              <div class="episodes-excerpt">
                                  <a href="<?php the_permalink() ?>" rel="bookmark">
                                      <?php echo excerpt(25); ?>
                                  </a>
                              </div>
                              <div class="episodes-feed-info-wrap">
                                  <div class="episodes-feed-author">
                                      <a href="<?php the_permalink() ?>" rel="bookmark">
                                        bloggy and storrieies
                                          <?php the_author(); ?>  &#8226; <?php echo meks_time_ago(); /* post date in time ago format */ ?>
                                      </a>
                                  </div>
                              </div>

                          </div><!-- end container -->
                      </div>

                    <?php endif; ?>
              </div>
          <?php endwhile;
          wp_reset_postdata();
          ?>
        </div>
      </div>
    </div><!-- end col 9 --> 
Share Improve this question edited Mar 24, 2020 at 20:56 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Mar 24, 2020 at 19:57 RLMRLM 2475 silver badges19 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Just wrap the opening and closing div like this:

<?php if ( ! in_array( 'daily-email', $post_terms, false ) ) : ?>
     <div class="row episodes-feed-wrap">

      ....... all your content and markup ........

     </div><!-- /this is your closing row div tag -->
<?php endif ?>

OR if you don't want that category to output at all, change the $args to this:

$catquery = new WP_Query( array(
    'orderby' => date,
    'order' => 'DESC',
    'tax_query' => array(
         array(
              'taxonomy' => 'category',
              'field' => 'slug',
              'terms' => 'daily-email',
              'operator' => 'NOT IN',
         )
     ),
));

That will remove all the daily-email posts from the loop.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论