Forums

Getting the post category slug in the loop? (4 posts)

  1. AJStacy
    Member
    Posted 3 months ago #

    I'm trying to get the slug of the current posts category while inside of the loop. I feel like pulling my hair out because something that should be simple is turning out to be a big pain.

    Is there something really easy I'm missing? How can I do this?

    Ultimately I'm using the category slug name to output to match with a css class name for styling purposes. Please help!

  2. esmi
    Theme Diva & Forum Moderator
    Posted 3 months ago #

    $category = get_the_category();
    $cat_=name =  $category[0]->cat_name;

    http://codex.wordpress.org/Function_Reference/get_the_category

  3. alchymyth
    The Sweeper
    Posted 3 months ago #

    if you would add post_class() to the post div, you would automatically get all sorts of category and tag specific css classes:

    http://codex.wordpress.org/Function_Reference/post_class

  4. AJStacy
    Member
    Posted 3 months ago #

    Thanks! This worked perfectly!

    $category = get_the_category();
    $cat_=name =  $category[0]->cat_name;

Reply

You must log in to post.

About this Topic