WordPress URL 按类别和标签显示 post

WordPress URL for showing post by both category and tag

如何使用 URL 显示具有特定类别 foo 和特定标签 bar 的所有帖子?

Example: site.com/category/foo/tag/bar

Example: site.com/posts?category={category-id}&tag={tag-id}

我不知道是否有默认功能,但我知道如何实现它。

You can create a custom template for this and in that template get the query string by get_query_var() and then use WP_Query cat and tag argument to fetch your posts belonging to that cat/tag.

这是一个示例工作代码:

$cat_id = get_query_var('category');
$tag_id = get_query_var('tag');
$args = [
    //...
    //...
    'post_type' => 'post', //<-- Replace it with your custom post_type
    'post_status' => 'publish',
    'tag_id' => $tag_id, //replace tag_id with tag if you are passing tag slug
    'cat ' => $cat_id //replace cat with category_name if your are passing category slug
    //...
    //...
];

// The Query
$query = new WP_Query($args);
if (!empty($query->posts))
{
    //print_r($query->posts);
    foreach ($query->posts as $post)
    {
        //Your filtered post
    }
}

希望对您有所帮助!

结构看起来像这样,但取决于您的永久链接结构。

/category/category-name/?tag=tag-name