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
Share Improve this question asked Dec 27, 2017 at 8:07 moonvadermoonvader 3325 silver badges17 bronze badges 2 |http://example/category/?cat_id={category_id}/
3 Answers
Reset to default 3If 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>
?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