WordPress网址用于显示帖子的类别和标签

问题描述:

如何使用URL显示特定类别的所有帖子foo和特定标签barWordPress网址用于显示帖子的类别和标签

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

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

我不知道现在还有没有作为该与否默认的功能,但我有一个想法,如何实现它。

可以为此创建一个自定义模板,在模板中通过get_query_var()得到 查询字符串,然后使用WP_Query cattag参数来获取您的文章属于 那只猫/标签。

下面是一个简单的工作代码:

$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 
    } 
} 

希望这有助于!

+0

非常感谢你 –

+0

@SyahnurNizam:如果它解决了你的问题,那么请接受我的答案_通过点击一个小tick_。 –