• Hannah S.L.

    (@fernashes)


    Automattic Happiness Engineer

    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)
  • I use <?php the_category() ?>

    Thread Starter Hannah S.L.

    (@fernashes)

    Automattic Happiness Engineer

    Huh? Oh, ack, did I miss the function part?

    Thread Starter Hannah S.L.

    (@fernashes)

    Automattic Happiness Engineer

    Thanks, Oriecat! 🙂 I was distracted by the & that was passed with the function as well.

    This barrier is gone! 🙂

    Thread Starter Hannah S.L.

    (@fernashes)

    Automattic Happiness Engineer

    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?

    Thread Starter Hannah S.L.

    (@fernashes)

    Automattic Happiness Engineer

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

    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)

    (@otto42)

    WordPress.org Admin

    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.

    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) for folks trying to determine how to get stuff out of the db.

    Thanks again…

    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…

    One last question, Otto….

    Is there a complete list somewhere of all the functions like get_the_category() etc??

    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)

    (@otto42)

    WordPress.org Admin

    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.