• I want to automate a certain functionality based on the categories that I add.

    If I have the category slug, how can I get it’s category ID?

    Thanks guys.

Viewing 5 replies - 16 through 20 (of 20 total)
  • bypan6 had the right answer though…

    $catid = get_cat_id(‘slug’);
    echo $catid;

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.remarpro.com Admin

    No, the get_cat_ID function gets a category by the name of the category, not by the slug.

    I have just tried and it worked ;D

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.remarpro.com Admin

    Only because you have the same slug as the category name is. If the slug had been different (like if you have a space in your category name, it’ll be a dash in the slug), then your code won’t work.

    Pretty old topic but this might be useful:
    get_category_by_slug(‘category-slug’)->term_id

    I’m using it in:
    <?php echo category_description(get_category_by_slug(‘category-slug’)->term_id); ?>

Viewing 5 replies - 16 through 20 (of 20 total)
  • The topic ‘get category ID from Slug’ is closed to new replies.