• 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)
  • Thread Starter tranceplant

    (@tranceplant)

    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.

    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 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.

    What is the problem with using $category[0]->cat_name as shown in example no. 3?

    Thread Starter tranceplant

    (@tranceplant)

    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

    <?php
    $category = get_the_category();
    if ( $category[0]->cat_name == 'FOOTBALL' ) {
       echo "hello world";
    }
    ?>
    Thread Starter tranceplant

    (@tranceplant)

    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 🙂

    Something like this perhaps?

    <?php
    $category = get_the_category();
    if ( $category[0]->cat_name == 'FOOTBALL' || 'SOCCER' ) {
       echo "hello world";
    }
    ?>
    Thread Starter tranceplant

    (@tranceplant)

    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!

    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 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

    (@tranceplant)

    ^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.