我有多个实例,其中wp_query显示相同分类术语链接(所有术语)的列表。每个实例都是唯一的,因为税务查询包含与其他分类术语的AND关系。
两个单独的查询都按相同的term-names / term_links列出
$terms = get_terms( 'term-001', 'orderby=name' );
$i = 0;
foreach ($terms as $term) {
$args = array(
'post_type' => 'post-type-001',
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'tax-002',
'terms' => 'term-001',
'field' => 'slug'
),
array(
'taxonomy' => 'tax-001',
'field' => 'slug',
'terms' => $term->slug
)
)
);
$term_link = get_term_link( $term );
$new2 = new WP_Query($args);
if($new2->have_posts()){
echo "<li><a href='".$term_link."'>".$term->name."</a></li>";
}
}
///////////////////
$terms = get_terms( 'term-001', 'orderby=name' );
$i = 0;
foreach ($terms as $term) {
$args = array(
'post_type' => 'post-type-001',
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'tax-002',
'terms' => 'term-002',
'field' => 'slug'
),
array(
'taxonomy' => 'tax-001',
'field' => 'slug',
'terms' => $term->slug
)
)
);
$term_link = get_term_link( $term );
$new2 = new WP_Query($args);
if($new2->have_posts()){
echo "<li><a href='".$term_link."'>".$term->name."</a></li>";
}
}
这是按预期工作的。问题是生成的术语链接全部转到相同的归档模板(taxonomy-tax-001)。我可以修改该存档模板(taxonomy-tax-001)来列出带有与我的第一个查询或我的第二个查询相匹配的参数的帖子,但是我无法理解如何从每个查询中获取术语链接以链接到存档将正确显示与其链接的查询中的完整条件匹配的帖子。
我尝试将自定义变量添加到列出术语的查询中。 'my_special_query'=&gt;是的,
$terms = get_terms( 'term-001', 'orderby=name' );
$i = 0;
foreach ($terms as $term) {
$args = array(
'post_type' => 'post-type-001',
'posts_per_page' => -1,
'my_special_query' => true,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'tax-002',
'terms' => 'term-002',
'field' => 'slug'
),
array(
'taxonomy' => 'tax-001',
'field' => 'slug',
'terms' => $term->slug
)
)
);
$term_link = get_term_link( $term );
$new2 = new WP_Query($args);
if($new2->have_posts()){
echo "<li><a href='".$term_link."'>".$term->name."</a></li>";
}
}
然后尝试在我的 functions.php 文件中附加基于自定义query_var的过滤器:
function new_query_vars( $query_vars ){
$query_vars[] = 'my_special_query';
return $query_vars;
}
add_filter( 'query_vars', 'new_query_vars' );
add_filter('template_include', 'alt_template');
function alt_template( $template ){
$terms = get_terms( 'tax-001');
$new = get_query_var('my_special_query');
global $wp_query;
foreach ($terms as $term) {
$term_link = get_term_link( $term );
if( isset($wp_query->query_vars['my_special_query'])) {
return locate_template('new-taxpage.php');
}
return $template;
}
}
这不会影响术语链接按预期打开的模板。