Title: Question &#8211; PHP variables
Last modified: August 18, 2016

---

# Question – PHP variables

 *  [Hannah S.L.](https://wordpress.org/support/users/fernashes/)
 * (@fernashes)
 * Automattic Happiness Engineer
 * [21 years, 2 months ago](https://wordpress.org/support/topic/question-php-variables/)
 * Hey all… question. I need to get a hold of the name of the category of each post
   but I can’t figure out how.
 * <?php the_category(‘ &’);?>
 * That’s the tag that I’m looking at… I’ve tried:
 * <?php $cat=the_category; ?>
 * But that only prints “the_category”, not the actual contents of the field. Any
   ideas?

Viewing 14 replies - 1 through 14 (of 14 total)

 *  [oriecat](https://wordpress.org/support/users/oriecat/)
 * (@oriecat)
 * [21 years, 2 months ago](https://wordpress.org/support/topic/question-php-variables/#post-152859)
 * I use <?php the_category() ?>
 *  Thread Starter [Hannah S.L.](https://wordpress.org/support/users/fernashes/)
 * (@fernashes)
 * Automattic Happiness Engineer
 * [21 years, 2 months ago](https://wordpress.org/support/topic/question-php-variables/#post-152861)
 * Huh? Oh, ack, did I miss the function part?
 *  Thread Starter [Hannah S.L.](https://wordpress.org/support/users/fernashes/)
 * (@fernashes)
 * Automattic Happiness Engineer
 * [21 years, 2 months ago](https://wordpress.org/support/topic/question-php-variables/#post-152865)
 * Thanks, Oriecat! 🙂 I was distracted by the & that was passed with the function
   as well.
 * This barrier is gone! 🙂
 *  Thread Starter [Hannah S.L.](https://wordpress.org/support/users/fernashes/)
 * (@fernashes)
 * Automattic Happiness Engineer
 * [21 years, 2 months ago](https://wordpress.org/support/topic/question-php-variables/#post-152913)
 * Ok, I thought that that had solved it… but it didn’t!
 * $cat=the_category();
    The line above always causes the category name to be printed…
   $cat is NOT filled.
 * Proof of that is this line:
    echo $cat.’blkaat’;
 * $cat isn’t printed, but that nonesense is.
 * Can somebody give me a hand with what exactly “the_category()” function is doing?
 *  [carthik](https://wordpress.org/support/users/carthik/)
 * (@carthik)
 * [21 years, 2 months ago](https://wordpress.org/support/topic/question-php-variables/#post-152932)
 * [http://codex.wordpress.org/Template_Tags/the_category](http://codex.wordpress.org/Template_Tags/the_category)
 *  Thread Starter [Hannah S.L.](https://wordpress.org/support/users/fernashes/)
 * (@fernashes)
 * Automattic Happiness Engineer
 * [21 years, 2 months ago](https://wordpress.org/support/topic/question-php-variables/#post-152940)
 * Thanks, 2fargon, but that’s unforuantely not the solution… all it does is tell
   different settings for customizing the output that you print on the screen. I
   need to know how to stuff it into a variable……….. and this doesn’t work either,
   unfortunately:
 * switch (the_category())
    { case ‘Whatever’: $icon=whatever; break;
 *  case “Blog”:
    $icon=blog; break;
 *  default:
    $icon=whoknows; break; }
 *  [salogan](https://wordpress.org/support/users/salogan/)
 * (@salogan)
 * [19 years, 6 months ago](https://wordpress.org/support/topic/question-php-variables/#post-153566)
 * Been tearing out what little hair I have left trying to do exactly the same thing.
 * I’m running a query based on a ‘calendar’ category (I’m using Event Calendar 
   3.1 which sorts things nicely in that category) and I then want to only display
   posts if they are member of another category. So I thought I’d simply find out
   what categories a post is in and do an ‘if’.
 * Anyone got any ideas how to get access in PHP to the categories of a post?
 * Thanks
 *  Moderator [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [19 years, 6 months ago](https://wordpress.org/support/topic/question-php-variables/#post-153567)
 * Depends on what you want to do.
 * Do you want to check the categories to see if a given post is in a category? 
   If so, you’d use get_the_category(), which returns an array of categories. Then
   you can check the cat_name on each one to see what the category name is.
 * Or if, like the original poster, you want a list of categories as a string, then
   you’d probably want get_the_category_list() instead.
 *  [salogan](https://wordpress.org/support/users/salogan/)
 * (@salogan)
 * [19 years, 6 months ago](https://wordpress.org/support/topic/question-php-variables/#post-153568)
 * Thanks Otto, your first guess is exactly what I did. I run get_the_category()
   and look through the returned array and display only those that are in the required
   category. The results is a nice ‘what’s on next’ page sorted correctly by date
   for one category.
 * I recommend this page ([http://codex.wordpress.org/Database_Description](http://codex.wordpress.org/Database_Description))
   for folks trying to determine how to get stuff out of the db.
 * Thanks again…
 *  [salogan](https://wordpress.org/support/users/salogan/)
 * (@salogan)
 * [19 years, 6 months ago](https://wordpress.org/support/topic/question-php-variables/#post-153569)
 * Just found an even easier way…
 * I want to print out posts in ascending date order for only one category, so…
 * (my calendar is category 2 and I only want entries from category 7)
 * `
    <?php query_posts('cat=2&orderby=date&order=DESC');  <?php if (have_posts()):?
   > <?php while (have_posts()) : the_post(); ?>  <?php if ( in_category(7) ) : ?
   > do stuff <?php endif; ?> <?php endwhile; ?> <?php endif; ?>
 * Works fine…
 *  [salogan](https://wordpress.org/support/users/salogan/)
 * (@salogan)
 * [19 years, 6 months ago](https://wordpress.org/support/topic/question-php-variables/#post-153570)
 * One last question, Otto….
 * Is there a complete list somewhere of all the functions like get_the_category()
   etc??
 *  [yansky](https://wordpress.org/support/users/yansky/)
 * (@yansky)
 * [19 years, 6 months ago](https://wordpress.org/support/topic/question-php-variables/#post-153571)
 * [http://codex.wordpress.org/Template_Tags](http://codex.wordpress.org/Template_Tags)
 *  [salogan](https://wordpress.org/support/users/salogan/)
 * (@salogan)
 * [19 years, 6 months ago](https://wordpress.org/support/topic/question-php-variables/#post-153572)
 * Otto42 mentioned a get_the_category_list() function that isn’t on the Template_Tags
   page? I can’t find any reference to it anywhere else. Otto – how come you know
   about it?!
 * Steve
 *  Moderator [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [19 years, 6 months ago](https://wordpress.org/support/topic/question-php-variables/#post-153573)
 * > Otto – how come you know about it?!
 * Because I rule. 😉
 * Actually, it’s because I eschew such minor things as documentation and just grep
   through the code for whatever I happen to want to find. I know about get_the_category_list()
   because I read the code for it in template-functions-category.php.

Viewing 14 replies - 1 through 14 (of 14 total)

The topic ‘Question – PHP variables’ is closed to new replies.

 * 14 replies
 * 6 participants
 * Last reply from: [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * Last activity: [19 years, 6 months ago](https://wordpress.org/support/topic/question-php-variables/#post-153573)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
