Title: simple PHP question
Last modified: August 21, 2016

---

# simple PHP question

 *  [tranceplant](https://wordpress.org/support/users/tranceplant/)
 * (@tranceplant)
 * [12 years ago](https://wordpress.org/support/topic/simple-php-question-6/)
 * Hello,
 * I hope I’m in the right section, but I would like to know if someone could help
   me with this simple PHP question related to wordpress obviously.
 * I am a beginner in PHP so I wrote this very simple line of code:
 * >  <?php
   >  $category = get_the_category(); if ( $category[0]->cat_ID == 8 ) { 
   > echo “hello world”; } ?>
 * As you can see if the ID = 8 it writes HELLO WORLD. I would like to know if it’s
   possible to use the actual name of the Category instead of the ID number.
 * If someone could help me out, that would be awesome
    Thanks guys

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

 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [12 years ago](https://wordpress.org/support/topic/simple-php-question-6/#post-4999160)
 * See the examples in [http://codex.wordpress.org/Function_Reference/get_the_category](http://codex.wordpress.org/Function_Reference/get_the_category)
 *  Thread Starter [tranceplant](https://wordpress.org/support/users/tranceplant/)
 * (@tranceplant)
 * [12 years ago](https://wordpress.org/support/topic/simple-php-question-6/#post-4999167)
 * HUmm, seriously I am not sure I understand the content of the link. Like I said
   I am a beginner.
 * I am not looking to echo the category name, I just want to use it in a “if” statement.
 *  [jayseae](https://wordpress.org/support/users/jayseae/)
 * (@jayseae)
 * [12 years ago](https://wordpress.org/support/topic/simple-php-question-6/#post-4999208)
 * What exactly are you wanting to accomplish?
 * Do you have cat_ID 8 and you want to find the category name that corresponds 
   to it?
 * – If so, then [get_cat_name](http://codex.wordpress.org/Function_Reference/get_cat_name)
   may do the trick for you. It returns the name of a category if you feed it a 
   given cat_ID.
 * Do you have some string and you want to find the cat_ID that corresponds to that?
 * – If so, then using **get_the_category**, as you do already, but with cat_name,
   instead of cat_ID, may be the way to go.
 * Do you want to do something else?
 * – Then that might require a little more information.
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [12 years ago](https://wordpress.org/support/topic/simple-php-question-6/#post-4999211)
 * What is the problem with using `$category[0]->cat_name` as shown in example no.
   3?
 *  Thread Starter [tranceplant](https://wordpress.org/support/users/tranceplant/)
 * (@tranceplant)
 * [12 years ago](https://wordpress.org/support/topic/simple-php-question-6/#post-4999217)
 * Thanks for your help guys, I appreciate it.
 * Like I said I am not a pro with PHP, to be honest I played around with this code
   and it worked, so my method is not exactly out of the book.
 * What I am looking to do here is when the user clicks a category that is named“
   football” for example. I want to write “hello world”
 * So let me write it the way I see it
 * <?php
    $category = get_the_category(); if ( $category[0]->cat_name == FOOTBALL){
   echo “hello world”; } ?>
 * ^I know this is wrong, but when a user creates a post, he simply needs to click
   the category FOOTBALL and the HELLO WORLD would show on the index page when reading.
 * I hope that make sense.
 * cheers
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [12 years ago](https://wordpress.org/support/topic/simple-php-question-6/#post-4999218)
 *     ```
       <?php
       $category = get_the_category();
       if ( $category[0]->cat_name == 'FOOTBALL' ) {
          echo "hello world";
       }
       ?>
       ```
   
 *  Thread Starter [tranceplant](https://wordpress.org/support/users/tranceplant/)
 * (@tranceplant)
 * [12 years ago](https://wordpress.org/support/topic/simple-php-question-6/#post-4999225)
 * oh yea, that did the trick. The only thing I was missing was the apostrophe.
 * Thanks a million.
 * I found what I was looking for and it’s perfect like that, but I’ll push my luck
   a little further.
 * I just realize that if I have 2 categories selected it wouldn’t work. Not a big
   deal, but would it be easy to have it say. if the name category is simply one
   of the many selected. Meaning, if I have football and hockey selected as a category
   the HELLO WORLD would still work.
 * Ok if it’s easy to do that would be great, if not, it will be all good.
 * Thanks again guys 🙂
 *  [jayseae](https://wordpress.org/support/users/jayseae/)
 * (@jayseae)
 * [12 years ago](https://wordpress.org/support/topic/simple-php-question-6/#post-4999229)
 * Something like this perhaps?
 *     ```
       <?php
       $category = get_the_category();
       if ( $category[0]->cat_name == 'FOOTBALL' || 'SOCCER' ) {
          echo "hello world";
       }
       ?>
       ```
   
 *  Thread Starter [tranceplant](https://wordpress.org/support/users/tranceplant/)
 * (@tranceplant)
 * [12 years ago](https://wordpress.org/support/topic/simple-php-question-6/#post-4999234)
 * oh thanks. I see what you did here. It does work.
 * but in both cases HELLO WORLD would appear.
 * What I was looking for is basically to have HELLO WORLD in all the posts that‘
   contains’ FOOTBALL. So SOCCER alone wouldn’t have the HELLO WORLD.
 * Thanks for your patience and time!
 *  [jayseae](https://wordpress.org/support/users/jayseae/)
 * (@jayseae)
 * [12 years ago](https://wordpress.org/support/topic/simple-php-question-6/#post-4999237)
 * Still not really sure what you’re after, so knowing exactly what you’re trying
   to produce could be helpful. 🙂
 * The link to [get_the_category](http://codex.wordpress.org/Function_Reference/get_the_category)
   may be where you want to spend some time.
 * The code above is only going to match on the first category assigned to your 
   post – that’s the [0], an index to the category array for the post. And yes, 
   an index of 0 pointing to the first item doesn’t really make sense. It’s just
   the way it works – not just in PHP, but in many programming languages.
 * So if the problem is that your posts will (potentially) have many categories,
   then perhaps you should loop through all of them, which would instead look more
   like this:
 *     ```
       <?php
       foreach((get_the_category()) as $category) {
       if ( $category->cat_name == 'FOOTBALL' ) {
          echo "hello world";
       }
       }
       ?>
       ```
   
 * The get_the_category actually returns an array, but rather than looking at only
   the first item (see, ma, no index!), it’s looping through them (foreach) instead.
   So if any one of them matches FOOTBALL, it’s going to echo ‘hello world’. This
   means no matter when FOOTBALL was added, it’ll show.
 * Before, if SOCCER (or HOCKEY, or any other category) was added, and FOOTBALL 
   was added later, so that it wasn’t the first one, then it wouldn’t match $category[
   0]. Thus, it wouldn’t work.*
 * Now, if you want to match multiple conditions, it’s going to get a little more
   complex.
 *     ```
       <?php
       foreach((get_the_category()) as $category) {
       if ( $category->cat_name == 'FOOTBALL' || 'HOCKEY' ) {
          echo "hello world";
       }
       }
       ?>
       ```
   
 * That’s FOOTBALL or HOCKEY.
 *     ```
       <?php
       foreach((get_the_category()) as $category) {
       if ( $category->cat_name == 'FOOTBALL' || 'SOCCER' ) {
          echo "hello world";
       }
       }
       ?>
       ```
   
 * That’s FOOTBALL or SOCCER.
 *     ```
       <?php
       foreach((get_the_category()) as $category) {
       if ( $category->cat_name == 'FOOTBALL' || 'HOCKEY' || 'SOCCER' ) {
          echo "hello world";
       }
       }
       ?>
       ```
   
 * That’s FOOTBALL or HOCKEY or SOCCER.
 *     ```
       <?php
       foreach((get_the_category()) as $category) {
       if ( $category->cat_name == 'FOOTBALL' && 'HOCKEY' ) {
          echo "hello world";
       }
       }
       ?>
       ```
   
 * That’s FOOTBALL and HOCKEY. Don’t do that – because you’re only looking at one
   category at a time, so you’ll never get a match.
 * * So you could also change the index – looking for $category[1] or something.
   But that just changes your problem. You would then be looking for FOOTBALL in
   the second category assigned (index 1 is the second item, just as index 0 is 
   the first item). So I don’t think it’s going to help.
 * Let me know if this does it for you.
 *  Thread Starter [tranceplant](https://wordpress.org/support/users/tranceplant/)
 * (@tranceplant)
 * [12 years ago](https://wordpress.org/support/topic/simple-php-question-6/#post-4999241)
 * ^Hey jay
 * that code was exactly what I was looking for. I feel i’m learning a bit here….
   But yeah I wish I knew PHP like you guys.
 * > <?php
   >  foreach((get_the_category()) as $category) { if ( $category->cat_name
   > == ‘FOOTBALL’ ) { echo “hello world”; } } ?>
 * Thanks again for your time and understanding. Your explaination was very good.
 * I’ll leave you guys alone now 🙂

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

The topic ‘simple PHP question’ is closed to new replies.

## Tags

 * [category](https://wordpress.org/support/topic-tag/category/)
 * [code](https://wordpress.org/support/topic-tag/code/)
 * [id](https://wordpress.org/support/topic-tag/id/)
 * [php](https://wordpress.org/support/topic-tag/php/)
 * [programing](https://wordpress.org/support/topic-tag/programing/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 11 replies
 * 3 participants
 * Last reply from: [tranceplant](https://wordpress.org/support/users/tranceplant/)
 * Last activity: [12 years ago](https://wordpress.org/support/topic/simple-php-question-6/#post-4999241)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
