0

Using this code from the codex:

       <?php
$category = get_the_category();
if($category[0]){
echo '<a href="'.get_category_link($category[0]->term_id ).'">'.$category[0]->cat_name.'</a>';
}
?>

Codex link: http://codex.wordpress.org/Function_Reference/get_the_category

I am able to display the category the post is in as a link (e.g. Business). However, I don't want to display the parent categories. I only want to display the child categories of a certain parent category.

My category structure is like this: Premium (Parent) > Premium Themes (Child) > Ecommerce (Child), Business (Child), Photography (Child), etc.

So I only want to display the children of the Premium Themes category.

Any help much appreciated.

1 Answer 1

1

your code may look like

<?php
$category = get_the_category();
foreach( $category as $cat):
  if($cat[0]->parent == ' your_parent_category_id ( Premium )'){
   echo '<a href="'.get_category_link($cat[0]->term_id ).'">'.$cat[0]->cat_name.'</a>';
  break;
  }
endforeach;
?>
2
  • Thanks, but I couldn't get that to work. But I've found a new snippet. Going to create a new thread so it's less confusing.
    – user34277
    Jun 19, 2013 at 20:54
  • as i visit your new thread, i updated my answer.i think it works for you and in this you do not need to call any additional wordpress function.just insert id of parent category in if condition Jun 20, 2013 at 4:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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