Uffo
Uffo

Reputation: 10046

Wordpress get posted in category permalink

So each post it will be posted in one category, and for each post I want to get the posted in category permalink, only the URL without markup. How can I do that?

//Explained again

Well the post is posted in a category, right? Well I need to show that category, but not the name of the category, the url for that category.

Upvotes: 0

Views: 1243

Answers (2)

user1081070
user1081070

Reputation: 49

No need to even place the anchor tag just place the following code, it will echo the URL and NAME as a permalink. It's build in already in WordPress.

<?php the_category(', ') ?>

Upvotes: 0

Matt Gibson
Matt Gibson

Reputation: 38238

I would use get_the_category(), which will return an array of objects, one for each category. In your case, you'll get an array containing one object back, because you only have one category. Then use get_category_link() to turn the ID of the category into a URL.

So, if you're in The Loop, I would do something like this (NB: untested!)

$categories = get_the_category();
$url = get_category_link($categories[0]->cat_ID);

Obviously, you'll want to make sure this doesn't fall on its backside if there are no categories assigned, for example.

This is similar to what WordPress does if you include a category in your permalink structure -- it will get the category with the lowest numeric ID and use that in the permalink.

Upvotes: 1

Related Questions