![](/img/trans.png)
[英]How to filter WordPress posts by Category first, then by Tags that belong to posts from the selected category?
[英]Getting tags related to posts only from selected category in Wordpress
我想使用get_terms
或其他方法仅获取与选定类别中的帖子相关的标签。 可能吗?
它实际上并不绑定到类别,而是绑定到该类别的帖子及其ID。 因此,您应该应该从类别索引页面的当前查询结果中获取数组中所有帖子ID的列表(查看一下查询循环的category.php或archive.php模板)示例:
$post_ids = array();
while (have_posts()) :
the_post();
$post_ids[] = get_the_ID();
endwhile;
。 并使用该wp_get_object_terms($post_ids, 'post_tag')
调用wp_get_object_terms($post_ids, 'post_tag')
,以从上方检索给定帖子ID的所有帖子标签。
可以使用特定的联接构建自定义的db查询,这可能会提高性能,但这是一种方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.