问题描述
经过几次不成功的搜索,我在这里问我的问题。 实际上,我正在尝试显示按类别分组的帖子列表:
CAT A
- post1
- post2
- post3
CAT B
- post4
- post5
- post6
- post7 ...
<?PHP
$terms = get_terms( 'secteur',array(
'orderby' => 'count','hide_empty' => 0
) );
foreach( $terms as $term ) {
$args = array(
'post_type' => 'client','posts_per_page' => '-1','secteur' => $term->slug
);
$query = new WP_Query( $args );
echo'<h3>' . $term->name . '</h3>';
// Start the Loop
while ( $query->have_posts() ) : $query->the_post();
$secteur_dactivite = get_field( 'secteur_dactivite' );
echo '<div class="cat-'.esc_html( $secteur_dactivite->slug ). '"><img src="'.get_field( 'logo' ).'"></div>';
endwhile;
wp_reset_postdata();
}
?>
解决方法
您需要使用 tax_query 作为 WP 查询属性,而不是 secteur。
尝试将其替换为:
$args = array(
'post_type' => 'client','posts_per_page' => '-1','tax_query' => array(
array(
'taxonomy' => 'secteur','field' => 'slug','terms' => $term -> slug,),);
,
非常感谢您的回复。 不幸的是,这不会改变显示。显示标题,但不显示文章。
<h3>CAT1</h3
<h3>CAT2</h3>
<h3>CAT3</h3>
如果有帮助,我可以使用以下代码显示所有项目:
<?php
$posts = get_posts(array(
'numberposts' => -1,'post_type' => 'client','post_status' => 'publish'
));
if($posts)
{
echo '<div class="row all-item">';
foreach($posts as $post)
{
echo '<div"><a href="'. get_permalink($post->ID) .'"><img src="'.get_field( 'logo' ).'"></a></div>';
}
echo '</div>';
}
?>
,
非常感谢您的回答。 但是没有任何效果对我有用,我想不出任何解决方案。 我认为问题在于我的分类法的配置。
这是我的自定义帖子类型配置(客户端): https://imgur.com/uDom3PH
我的分类配置(secteur): https://imgur.com/WRwsSbR
我的自定义字段 (secteur_dactivite): https://imgur.com/NKQ4GPn
再次感谢您的帮助