最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

categories - Get category URL knowing it's id

programmeradmin0浏览0评论

I have id of WP category on external resource. I know that if I have id of post I can create url like

/?p={post_id}

But what if I know id of category? How can I generate link to it? Category permalinks look like

/{category_slug}/

and I need to use something like

/?cat_id={category_id}/

I have id of WP category on external resource. I know that if I have id of post I can create url like

http://example/?p={post_id}

But what if I know id of category? How can I generate link to it? Category permalinks look like

http://example/category/{category_slug}/

and I need to use something like

http://example/category/?cat_id={category_id}/

Share Improve this question asked Dec 27, 2017 at 8:07 moonvadermoonvader 3325 silver badges17 bronze badges 2
  • Set Permalinks to Plain and visit a category to see for yourself. – Jacob Peattie Commented Dec 27, 2017 at 9:42
  • Custom taxonomy: you can do ?taxonomy_key=a-term-slug e.g. ?sport_category=some-ball-game , I had no luck of finding a way with just term_id ... – jave.web Commented May 3, 2021 at 0:01
Add a comment  | 

3 Answers 3

Reset to default 3

If you have category ID you can create link to category like below :

<a href="/index.php?cat=7">Category Title</a>

For more details read from this link :

https://codex.wordpress/Linking_Posts_Pages_and_Categories

Thanks!

use get_term_link:

print get_term_link( $category_id, $taxonomy );

If you have the category id then you can easily get the category URL using the below code.

get_category_link( $category_id );

Example:

<a href="<?php echo esc_url( get_category_link( $category_id) ); ?>"  class="view_more_button">View More</a>
发布评论

评论列表(0)

  1. 暂无评论