【发布时间】: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