WordPress.org

Ready to get started?Download WordPress

Forums

Question - PHP variables (15 posts)

  1. fernashes
    Member
    Posted 9 years ago #

    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?

  2. oriecat
    Member
    Posted 9 years ago #

    I use <?php the_category() ?>

  3. fernashes
    Member
    Posted 9 years ago #

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

  4. fernashes
    Member
    Posted 9 years ago #

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

    This barrier is gone! :)

  5. fernashes
    Member
    Posted 9 years ago #

    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?

  6. carthik
    Member
    Posted 9 years ago #

  7. fernashes
    Member
    Posted 9 years ago #

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

  8. salogan
    Member
    Posted 7 years ago #

    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

  9. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    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.

  10. salogan
    Member
    Posted 7 years ago #

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

  11. salogan
    Member
    Posted 7 years ago #

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

  12. salogan
    Member
    Posted 7 years ago #

    One last question, Otto....

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

  13. Yansky
    Member
    Posted 7 years ago #

  14. salogan
    Member
    Posted 7 years ago #

    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

  15. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.