【发布时间】:2017-09-23 10:24:31
【问题描述】:
如何使用 URL 显示具有特定类别 foo 和特定标签 bar 的所有帖子?
Example: site.com/category/foo/tag/bar
或
Example: site.com/posts?category={category-id}&tag={tag-id}
【问题讨论】:
标签: php wordpress url tags categories
如何使用 URL 显示具有特定类别 foo 和特定标签 bar 的所有帖子?
Example: site.com/category/foo/tag/bar
或
Example: site.com/posts?category={category-id}&tag={tag-id}
【问题讨论】:
标签: php wordpress url tags categories
我不知道这是否有默认功能,但我知道如何实现它。
您可以为此创建一个自定义模板,然后在该模板中获取 通过
get_query_var()查询字符串,然后使用 WP_Querycat和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
}
}
希望这会有所帮助!
【讨论】:
get_query_var('cat') 返回 category_id,get_query_var('category_name') 返回 category_name。 get_query_var('tag') 返回 tag_name,get_query_var('tag_id') 返回 tag_id。 get_query_var('category') 不返回任何内容。
get_query_var('abc')
结构看起来像这样,但取决于您的永久链接结构。
/category/category-name/?tag=tag-name
【讨论】:
example.com/%postname% 您还应该保持英文本地化,或者您还应该更改 url 中的“类别”(例如意大利语是:example.com//**categoria**/_category-slug_/?tag=_tag-slug-1_,_tag-slug-2_)