关于我的设置的Info:
我正在尝试实现的What:
在治疗类别存档页面上显示治疗,但按品牌分类法划分,还包括未附加到品牌分类法的类别内的治疗。所以档案页面应该是这样的:
Treatment类别标题
Brand 1
Brand 2
(无品牌)
我在每个分类法页面上都做了一个taxonomy.php模板,但是.
My问题是:
1)我无法找到一种方法来阻止空处理类别标题的显示。我认为我需要在某个地方插入一个foreach或IF语句,或者可能更改$args数组,以便只返回包含处理的类别。我尝试过包括hide_empty语句,但这似乎也不起作用。
2)当我根据品牌分类设置查询时,我不能将不附加于任何品牌的处理显示出来。也许我需要两个循环?
这是我的密码,有什么帮助吗?
taxonomy;
$term_id = $queried_object->term_id;
// fetch the terms for brands tax
$terms = get_terms( 'brands', array(
'orderby' => 'title',
'order' => 'ASC',
'hide_empty' => true,
) );
the_archive_title( '', '' );
// now run a query for each brand
foreach( $terms as $term ) {
$args = array(
'post_type' => 'treatments',
'nopaging' => true,
'orderby' => 'title',
'order' => 'ASC',
'hide_empty' => true,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'brands',
'terms' => $term->slug,
'field' => 'slug',
'operator' => 'IN'
),
array(
'relation' => 'OR',
array(
'taxonomy' => 'beautysalon',
'terms' => $queried_object->slug,
'field' => 'slug',
'operator' => 'IN'
),
array(
'taxonomy' => 'skinclinic',
'terms' => $queried_object->slug,
'field' => 'slug',
'operator' => 'IN'
),
array(
'taxonomy' => 'spadays',
'terms' => $queried_object->slug,
'field' => 'slug',
'operator' => 'IN'
),
)
)
);
$query = new WP_Query( $args );
// output the term name in a heading tag
echo'' . $term->name . '';
// output the post titles in a list
echo '';
while ( $query->have_posts() ) : $query->the_post(); ?>
';
wp_reset_postdata();
} ?>发布于 2018-03-26 09:46:15
1)修复这个问题非常简单--我用if have_posts包装了类别名称请求,从而去掉了空值。
2)我必须包括一个使用'operator' => 'NOT IN'的次级循环,以列出不属于分类法的治疗方法。
我相信有一个更精简和更好的方法来做下面的事情,但就目前而言,这为我想做的事情服务。
最终代码
taxonomy;
$term_id = $queried_object->term_id;
// fetch the terms for brands tax
$terms = get_terms( 'brands', array(
'orderby' => 'title',
'order' => 'ASC',
'hide_empty' => true,
'posts_per_page' => -1,
) );
the_archive_title( '', '' );
// now run a query for each brand
foreach( $terms as $term ) {
$args = array(
'post_type' => 'treatments',
'nopaging' => true,
'orderby' => 'title',
'order' => 'ASC',
'hide_empty' => true,
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'brands',
'terms' => $term->slug,
'field' => 'slug',
'operator' => 'IN',
'hide_empty' => true,
),
array(
'relation' => 'OR',
array(
'taxonomy' => 'beautysalon',
'terms' => $queried_object->slug,
'field' => 'slug',
'operator' => 'IN'
),
array(
'taxonomy' => 'skinclinic',
'terms' => $queried_object->slug,
'field' => 'slug',
'operator' => 'IN'
),
array(
'taxonomy' => 'spadays',
'terms' => $queried_object->slug,
'field' => 'slug',
'operator' => 'IN'
),
)
)
);
$query = new WP_Query( $args );
// only show a header if there is posts present
if( $query->have_posts() ) :
echo'' . $term->name . '';
echo '';
while ( $query->have_posts() ) :
$query->the_post(); ?>
';
endif;
wp_reset_postdata();
}
// second query - everything not in a brand
$args = array(
'post_type' => 'treatments',
'nopaging' => true,
'orderby' => 'title',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'brands',
'terms' => $term->slug,
'field' => 'term_id',
'operator' => 'NOT IN',
),
array(
'relation' => 'OR',
array(
'taxonomy' => 'beautysalon',
'terms' => $queried_object->slug,
'field' => 'slug',
'operator' => 'IN'
),
array(
'taxonomy' => 'skinclinic',
'terms' => $queried_object->slug,
'field' => 'slug',
'operator' => 'IN'
),
array(
'taxonomy' => 'spadays',
'terms' => $queried_object->slug,
'field' => 'slug',
'operator' => 'IN'
),
)
));
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
echo 'Other ' . $queried_object->name . ' Treatments';
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
';
wp_reset_postdata();
?>https://wordpress.stackexchange.com/questions/298900
复制相似问题