0

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.com/?p={post_id}

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

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

and I need to use something like

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

  • Set Permalinks to Plain and visit a category to see for yourself. – Jacob Peattie Dec 27 '17 at 9:42
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.org/Linking_Posts_Pages_and_Categories

Thanks!

1

use get_term_link:

print get_term_link( $category_id, $taxonomy );
  • I cannot use WP functions because I generating URLs on external resource – moonvader Dec 27 '17 at 8:21
  • You could use ?category_name=$slug and let WP do a redirect to the actual permalink for you, if that's OK, or does it have to rely on the ID? – janh Dec 27 '17 at 8:50
  • I don't have slug I have only list with ids of categories. If I had slug i could generate url example.com/category/$slug but it is not my case – moonvader Dec 27 '17 at 9:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.