admin管理员组文章数量:1336437
I have a variable that contains the slug of a category. With that slug I want to create a link to the archive page that displays all the posts in that specific category. What is the best or fastest way to go from slug to category URL?
Preferably I don't want to use a lot of variables (like when going from slug > id > url, each with a different variable). I would like a function that does one of the following, to keep the file as clean as possible.
echo get_url_function(slug)
or echo get_url_function( get_id_function (slug) )
Thanks a lot in advance!
I have a variable that contains the slug of a category. With that slug I want to create a link to the archive page that displays all the posts in that specific category. What is the best or fastest way to go from slug to category URL?
Preferably I don't want to use a lot of variables (like when going from slug > id > url, each with a different variable). I would like a function that does one of the following, to keep the file as clean as possible.
echo get_url_function(slug)
or echo get_url_function( get_id_function (slug) )
Thanks a lot in advance!
Share Improve this question asked Jun 26, 2020 at 18:20 ralphjsmitralphjsmit 4026 silver badges23 bronze badges1 Answer
Reset to default 3The WP function get_category_link()
will do the trick.
/**
* Gets the URL for a category term archive based on the category's slug.
*
* @param string $category_slug The slug of the category to get the category arcive for.
*
* @return string The category (term) archive URL. Empty string on error.
*/
function wpse_get_category_url_by_slug( $category_slug ) {
return get_category_link( get_cat_ID( $category_slug ) );
}
Instead of a creating a wrapper like the example above, you could also just call the WP function directly: get_category_link( get_cat_ID( $category_slug ) )
本文标签: categoriesHow to get category URL with the slug
版权声明:本文标题:categories - How to get category URL with the slug? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742313747a2451395.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论