如果为空,则在循环中隐藏一行

时间:2020-03-24 作者:RLM

我试图隐藏具有特定类别的帖子,但当一篇帖子被隐藏时,它会显示为空行。我试图绕过我的条件语句,但它只是破坏了我的逻辑或页面结构。

如何隐藏行(<div class"row episodes-feed-wrap"> ) 如果类别为\'daily-email\'?

这是我的代码:

<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 --> 

1 个回复
最合适的回答,由SO网友:disinfor 整理而成

只需像这样包装开始和结束div:

<?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 ?>
或者,如果您根本不想输出该类别,请更改$args 对此:

$catquery = new WP_Query( array(
    \'orderby\' => date,
    \'order\' => \'DESC\',
    \'tax_query\' => array(
         array(
              \'taxonomy\' => \'category\',
              \'field\' => \'slug\',
              \'terms\' => \'daily-email\',
              \'operator\' => \'NOT IN\',
         )
     ),
));
这将删除所有daily-email 来自循环的帖子。

相关推荐

WP ForLoop用于比较帖子的元数据信息以确定要显示的帖子

我在这里尝试的缺点是:我只想根据存储在自定义字段中的日期显示一篇文章。我只想显示符合两个条件的帖子1:日期必须尚未发生,即必须大于当前日期2:要显示的帖子日期必须是所有可能帖子中最接近当前日期的。这是下一个日期。我的问题是,当我要比较一篇文章和另一篇文章,然后使用WordPress循环只显示适当的文章时,我不知道从哪里开始。我想我需要使用for循环遍历每个帖子并进行比较,然后运行一个新的查询来显示我的一篇帖子。太长,读不下去了我可以使用forloop来检查WP\\u查询吗?如何访问其中的帖子信息?这是我的