WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to add a back button that goes to the parent category of the actual single post that the user is viewing. How can I do that?

<h2 class="link"><a href="#">Return</a></h2>

Thank you.

share|improve this question
What should happen if there are multiple categories? – toscho Nov 14 '12 at 23:45
there are a lot of categories but every single post belong to one and not to two or more – wpuser Nov 14 '12 at 23:47

You can use get_the_category() and return the first result as in the example. Technically you could have several parent categories so you need to deal with this separately or expect that the first element of the array is always the right parent category.

share|improve this answer
get_the_category just get's the link of the same post... – wpuser Nov 14 '12 at 23:32
Are you giving the $post->ID parameter to get_the_category codex.wordpress.org/Function_Reference/get_the_category – kidakaka Nov 15 '12 at 5:42

Paste this code in single.php according to your theme style

    foreach($cats as $cat){
/*check for category having parent or not except category id=1 which is wordpress default category (Uncategorized)*/
        if($cat->category_parent == 0 && $cat->term_id != 1){
            echo '<h2 class="link"><a href="'.get_category_link($cat->term_id ).'">Return</a></h2>';

Output HTML form this code:

<h2 class="link"><a href="(Link to first category which not have any parent category)">Return</a></h2>

Important Link: get_the_category

share|improve this answer
<h2 class="link"><a href="<?php $cat = get_the_category(); $cat = $cat[0]; echo get_category_link($cat->cat_ID); ?>">Return</a></h2>

This should work!

share|improve this answer
Please add an explanation to your answer: why could that solve the problem? – toscho Jul 23 '13 at 11:10

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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