I created a simple function to display the latest posts. Inside the function there is a conditional to display the post category. I found that to display the category name, using

$category = get_the_category($post->post_ID); 

$category[0]->cat_name; // I want to display only the first category

do the job, but can't find how to display the link to the category. Tried to use get_category_link($post->post_ID) but no changes, the link still redirects to the current page. The goal is to redirect to category archive.

Appreciate any help.


If you can add your code, It may help for everyone. By reading you question. By default normally that get_category_link is have to work. Just use global $post before your loop. If is not work. What you have to do is.

  1. Grab the category id using your @category variable. (you can use print_r($category); or var_dump($category); after your code $category = get_the_category($post->post_ID);)

  2. Then use get_category_link(pass your grabbed category id)

I'm sure it will work.

For more info. Read this CODEX

  • I am happy. This is work for you. Please can you vote up. :-D – yeshansachithak Apr 16 '14 at 9:37

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.