wordpress - 仅显示在 Wordpress 中有帖子的术语
问题描述
我有一个适用于两种分类法的 Wordpress 帖子类型。我想显示分类法“locationcat”中所有术语的列表,该列表使用其他分类法“menucat”中的术语“午餐”。可悲的是,我已经能够打印两种分类法的所有术语。这是我的代码:
$terms = get_terms( array(
'hide_empty' => true,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'locationcat',
'hide_empty' => true,
),
array(
'taxonomy' => 'menucat',
'field' => 'slug',
'terms' => 'lunch',
'hide_empty' => true,
),
),
) );
foreach ($terms as $sc) {
$link = $sc->slug;
echo '<li><a href="#'. $link .'">'.$sc->name.'</a></li>';
}
解决方案
当您询问“仅显示在 Wordpress 中有帖子的条款”时,您的帖子有点令人困惑。我假设您的意思是“使用来自 2 个分类法的术语显示帖子”。
如果这是正确的,那么这就是您的解决方案。
$args = array(
'post_type' => 'your_post_type',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'tax_1'
'field' => 'slug',
'terms' => 'term-slug1'
),
array(
'taxonomy' => 'tax_2',
'field' => 'slug',
'terms' => 'term-slug1'
)
)
);
$posts = get_posts( $args );
然后继续你的循环。
如果我误解了您的问题,请尝试改写它。
推荐阅读
- java - 在任何扩展名之前添加日期到我的文件名
- php - JWT/OAuth 令牌签名验证失败
- jenkins - 如何根据 os.name 选择节点代理
- python - 常量上的 PyFMI FMU 模块方法 get_variable_unit()
- r - 如何使用 R 快速绘制所有变量
- excel - 文件无法识别宏的存在
- javascript - 在 map 函数中分配一个 const 变量
- c - 尝试访问动态指针数组时的 SIGSEV (C)
- python - Django 2 命名空间和 app_name
- python - Selenium Python:move_to_element 不移动