0

I am working on a theme that displays a single category for each post.

However to eliminate user error, I would like to pluck a single parent category out of a posts categories.

Here is my code as of now:

   function thb_DisplaySingleCategory($boxed = true, $id = false) { 
$categories = get_the_category();
$_id = (empty($id) ? $categories[0]->cat_ID : $id);
$_name = (empty($id) ? $categories[0]->cat_name : get_cat_name($id));

if ($boxed) {
    $thecategory = '<a href="'.get_category_link($_id).'" class="boxed" title="'.$_name.'" style="background:'.GetCategoryColor($_id).'; ">'.$_name.'</a>';
} else {
    $thecategory = '<a href="'.get_category_link($_id).'" class="" title="'.$_name.'" style="color:'.GetCategoryColor($_id).'; ">'.$_name.'</a>';
}


return $thecategory;
}

This gives me a single category, but it can be children.

0

You can check the category 'parent' arribute, and if is 0 the category is a parent one.

I've also noted some things on your code:

  1. When the is is passed get_the_category is unused, but it is called anyway
  2. You use get_the_category assuming function are called in the loop, but there is no check for that
  3. If, for any case, get_the_category return no categories, your function thrown errors
  4. Before inserting retrieved values inside html shoul be escaperd with esc_* functions

I rewrote your function considering all those things and adding the check for parent category:

function thb_DisplaySingleCategory($boxed = true, $id = false) { 
  if ( (int) $id ) {
    $cat = get_category($id);
    if ( ! empty($cat) ) {
       $backup = $cat->term_id;
       if ( $cat->parent == 0 ) $id = $cat->term_id;
    }
  }
  if ( empty($id) && in_the_loop() ) {
    $id = '';
    $categories = get_the_category();
    if ( empty( $categories ) ) return;
    while ( empty($id) && ! empty($categories) ) {
      $cat = array_shift($categories);
      if ( $cat->parent == 0 ) $id = $cat->term_id;
    }
  }
  // if no parent cat found, but a not-parent cat id passed to function use that
  if ( ! (int) $id && isset($backup) ) $id = $backup;
  if ( ! (int) $id ) return;
  $name = get_cat_name($id);
  $url = esc_url( get_category_link($id) );
  $wcolor = $boxed ? 'background' : 'color';
  $class = $boxed ? ' class="boxed"' : '';
  $color = GetCategoryColor($id);
  $frmt = '<a href="%s"%s title="%s" style="%s:%s;">%s</a>';
  return sprintf( $frmt, $url, $class, esc_attr($name), $wcolor, $color, esc_html($name) );
}
| improve this answer | |
  • Thanks for the response @G.M. However, when I used your code, it does not return any category for some posts that have multiple parent categories and children categories assigned. Thanks for pointing some of the mistakes as well, as I am learning PHP. – Ant Eksiler Oct 25 '13 at 8:06
  • After further checking, I found that the problem is with the boxed attribute. If the function is called like this: echo thb_DisplaySingleCategory(false) No Category is displayed. – Ant Eksiler Oct 25 '13 at 8:12
  • @AntEksiler I've tested with both $boxed = true and $boxed = false and it works form me. I can't full test because I don't know what GetCategoryColor do, for test I just put $color = '#F00'; and it works... – gmazzap Oct 25 '13 at 12:27

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.