【问题标题】:Else no posts found in foreach loop with while否则在 foreach 循环中找不到帖子
【发布时间】:2017-07-13 21:26:23
【问题描述】:

这是一个带有分类术语的 CPT 档案,我无法显示“未找到帖子”?该循环有效,它将每个术语名称显示为标题,并在列表中显示每个术语下的帖子。我已经尝试了很多代码来显示“未找到帖子”...没有任何效果?

<?php


//fetch the terms for the policy taxonomy
  $terms = get_terms( 'policy-groups', array(
  'hide_empty' => 'true',
) );

// run a query for each policy-group term
foreach( $terms as $term ) :
   $args = array(
    'post_type'  => 'policies',
    'policy-groups' => $term->slug ,
    'order'        => 'DSC',
    'posts_per_page' => -1,
);

  $query = new WP_Query( $args );  
     if( $query->have_posts() ) :  ?>

       <ul>

         <?php
         // output the policy-group name in a heading tag               
         echo'<h4 class="policy-archive-heading">' . $term->name . '</h4>'; 

         // Start while the Loop   
          while ( $query->have_posts() ) : $query->the_post(); 
             $attachment_id = get_field( "policy_upload" );
             $url = wp_get_attachment_url( $attachment_id );
          ?>

            <li>
                <a href="<?php echo $url; ?>" target="_blank"><?php the_title(  ); ?></a>
            </li>  


<?php endwhile; //endwhile ?>

<?php else: printf( __('<strong>Sorry , No posts were found</strong>')); ?>

<?php endif; //end if posts ?>

</ul>

<?php endforeach ?>


// use reset postdata to restore orginal query
wp_reset_postdata();

【问题讨论】:

  • 我认为这行不通,因为'IF'没有帖子它永远不会到达'Else'?

标签: php wordpress loops foreach while-loop


【解决方案1】:

您没有显示没有poststerms。要显示没有帖子的条款,请更改代码中的'hide_empty' =&gt; 'false'

请找到您更新后的 $terms 查询代码:

$terms = get_terms( 'policy-groups', array(
  'hide_empty' => false,
) );

还要更正您的 $args 查询,请在下面找到代码:

$args = array(
        'post_type'  => 'policies',
        'post_status' => 'publish',
        'tax-query'   => array(
              array( 
                 'taxonomy' => 'policy-groups',
                 'field' => 'slug',
                 'term' => $term->slug,
              )
         ),
        'orderby'     =>  'title',
        'order'        => 'ASC',
        'posts_per_page' => -1,
    );

还将开始的'&lt;ul&gt;' 标记放在if( $query-&gt;have_posts() ) 条件之前或将结束的'&lt;/ul&gt;' 放在'endwhile;' 之后,因为它们没有正确同步。

所有更正后,这是您的完整代码,复制并替换为您的代码:

<?php
//fetch the terms for the policy taxonomy
  $terms = get_terms( 'policy-groups', array(
  'hide_empty' => false,
) );

// run a query for each policy-group term
foreach( $terms as $term ) :
   $args = array(
        'post_type'  => 'policies',
        'post_status' => 'publish',
        'tax-query'   => array(
              array( 
                 'taxonomy' => 'policy-groups',
                 'field' => 'slug',
                 'term' => $term->slug,
              )
         ),
        'orderby'     =>  'title',
        'order'        => 'ASC',
        'posts_per_page' => -1,
    );

    $query = new WP_Query( $args );  ?>
    <ul>
        <?php if( $query->have_posts() ) :  
            // output the policy-group name in a heading tag               
            echo'<h4 class="policy-archive-heading">' . $term->name . '</h4>'; 

            // Start while the Loop   
            while ( $query->have_posts() ) : $query->the_post(); 
                $attachment_id = get_field( "policy_upload" );
                $url = wp_get_attachment_url( $attachment_id ); ?>
                <li>
                    <a href="<?php echo $url; ?>" target="_blank"><?php the_title(  ); ?></a>
                </li>  


            <?php endwhile; //endwhile ?>

        <?php else: printf( __('<strong>Sorry , No posts were found</strong>')); ?>

        <?php endif; //end if posts ?>

    </ul>

<?php endforeach ;
// use reset postdata to restore orginal query
wp_reset_postdata(); ?>

请根据您在上述查询中的要求更改'order' =&gt; 'ASC/DESC'

希望对你有帮助。

【讨论】:

  • 感谢您的尝试,但这不起作用。如果我使用您编辑的代码,实际的术语标题会正确显示,但它们下方列表中的帖子显示所有帖子,无论它们分配的术语是什么。在每个术语标题下应该只有分配给该策略组的术语。我的原始代码可以实现这一点,但没有显示“找不到帖子”。此外,您的代码也不会显示“未找到帖子”。
  • 对不起兄弟,我错了,请把'false'中的单引号('')去掉,写成'hide_empty' => false。请尝试一次。我已经更新了代码。
  • 感谢您的尝试,但结果也一样。
【解决方案2】:

我会改变几件事:

  • 按词条 ID 搜索
  • 仅发布帖子
  • 将重置后的数据移动到正确的位置
  • 按 DESC 顺序输入错误

代码如下:

 <?php


    //fetch the terms for the policy taxonomy
      $terms = get_terms( 'policy-groups', array(
      'hide_empty' => 'true',
    ) );

    // run a query for each policy-group term
    foreach( $terms as $term ) :
       $args = array(
        'post_type'  => 'policies',
        'post_status' => 'publish',
        'tax-query'   => array(
              array( 
                 'taxonomy' => 'policy-groups',
                 'term' => $term->term_id
              )

        ),
        'order'        => 'DSC',
        'posts_per_page' => -1,
    );

      $query = new WP_Query( $args );  
         if( $query->have_posts() ) :  ?>

           <ul>

             <?php
             // output the policy-group name in a heading tag               
             echo'<h4 class="policy-archive-heading">' . $term->name . '</h4>'; 

             // Start while the Loop   
              while ( $query->have_posts() ) : $query->the_post(); 
                 $attachment_id = get_field( "policy_upload" );
                 $url = wp_get_attachment_url( $attachment_id );
              ?>

                <li>
                    <a href="<?php echo $url; ?>" target="_blank"><?php the_title(  ); ?></a>
                </li>  


    <?php endwhile; //endwhile ?>

    <?php else: printf( __('<strong>Sorry , No posts were found</strong>')); ?>

    <?php endif; //end if posts ?>

    </ul>

    <?php 
      // use reset postdata to restore orginal query
      wp_reset_postdata();
         endforeach; ?>

【讨论】:

  • 感谢您的尝试,但这也不起作用,结果与我对 Prateek Verma 的评论相同
猜你喜欢
  • 2017-07-04
  • 2011-06-13
  • 1970-01-01
  • 2015-03-27
  • 2020-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多