Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

In several theme templates I have code to retrieve the category name and slug for the present post or category page. It is outside the loop. Here is the repeated code:

global $post;
$categories = get_the_category($post->ID);
if ( $categories ) {
foreach ($categories as $category) {
    $dw_category_slug = $category->slug;
    $dw_category_name = $category->name;
}  
}else {
    $dw_category_slug = 'utopia';
} 

I would like to be able to include this in the functions file instead of writing it in several places. This is what I have tried in my functions file:

  add_action('wp_head', 'dw_get_category');
  function dw_get_category() {
    global $post;
    $categories = get_the_category($post->ID);
           if ( $categories ) {
               foreach ($categories as $category) {
               $dw_category_slug = $category->slug;
               $dw_category_name = $category->name;
        }
        } else {
            $dw_category_slug = 'utopia';
        }         
    $dw_category[] = array('name' => $dw_category_name, 'slug' =>    $dw_category_slug);
    return $dw_category;     
    }                                                   

I would like to be able to write something like $dw_category['name'] in the template files to call the category name. I know this is the wrong way to do it. Do I need to divide this into 2 functions so there is only one value returned for the function? Or is there some other way to do this? Writing it as a class?

If interested the url is http://lenoremalenblog.com

Thanks for your time.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

You don't need to hook the dw_get_category() function. Simply do it like this:

  function dw_get_category() {
    global $post;
    $categories = get_the_category($post->ID);
        if ( $categories ) {
           foreach ($categories as $category) {
               $dw_category_slug = $category->slug;
               $dw_category_name = $category->name;
           }
        } else {
            $dw_category_slug = 'utopia';
        }         
    $dw_category = array('name' => $dw_category_name, 'slug' =>    $dw_category_slug);
    return $dw_category;     
    } 

Then call it like this:

$the_post_cats = dw_get_category();
echo 'Category: '.$the_post_cats['name'];
echo 'Slug: '.$the_post_cats['slug'];
share|improve this answer
    
thanks, that worked. –  danielwiener Jul 8 '11 at 13:04
    
thanks, that worked. I also had to get rid of the '[]' in $dw_category[], which was a careless mistake. Thanks –  danielwiener Jul 8 '11 at 13:17
    
@danielwiener Yes, the [] was not necessary (see code - it's copy/paste ready). So: Glad it worked. :) –  kaiser Jul 8 '11 at 17:08
add comment

Your Answer

 
discard

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.