这是在带有分类条款的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();
答案 0 :(得分:1)
您没有显示没有terms
的{{1}}。要显示没有帖子的字词,请更改代码中的posts
。
请查找$ terms query的更新代码:
'hide_empty' => 'false'
并更正您的$ args查询,请找到以下代码:
$terms = get_terms( 'policy-groups', array(
'hide_empty' => false,
) );
并将开头$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,
);
标记放在'<ul>'
条件之前,或将结束if( $query->have_posts() )
放在&{39; '</ul>'
&#39;之后,因为它们是没有正确同步。
完成所有更正后,这里是您的完整代码,副本和&amp;替换为您的代码:
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(); ?>
。
我希望,这对你有帮助。
答案 1 :(得分:0)
我会改变一些事情:
以下代码:
<?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; ?>