dongxixian7803 2017-02-23 11:53
浏览 29

否则,在foreach循环中找不到帖子

This is on a CPT archive with Taxonomy terms , i can't get the 'No posts found' to show up ? the loop works , it displays each term name as a heading and shows the posts under each term in a list. I have tried a lot of code to get the 'No posts found' to show up ... nothing is working ?

<?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();
  • 写回答

2条回答 默认 最新

  • doushang8512 2017-02-23 12:03
    关注

    You are not displaying the terms which have no posts. For showing the terms having no posts, change the 'hide_empty' => 'false' in your code.

    Please find your updated code of $terms query:

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

    And also correct your $args query, please find the code below:

    $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,
        );
    

    And also place the opening '<ul>' tag before the if( $query->have_posts() ) condition or place the closing '</ul>' just after 'endwhile;', as they are not properly synchronized.

    After all the correction, here is your full code, copy & replace with your code:

    <?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(); ?>
    

    Please change the 'order' => 'ASC/DESC' as per your requirement in the above queries.

    I hope, it may be helpful to you.

    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?