WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/get category by slug

Description

Retrieve category object by category slug. Returns false if not found.

Usage

<?php get_category_by_slug$slug ); ?>

Note: same is achieved by "get_term_by('slug', $slug, 'category')"

Parameters

$slug
(string) (required) The category slug.
Default: None

Return Values

(object) 
Category data object.
(boolean) 
false if not found

Examples

<?php 
  $idObj = get_category_by_slug('category-slug'); 
  $id = $idObj->term_id;
?>

Notes

Change Log

Since: 2.3.0

Source File

get_category_by_slug() is located in wp-includes/category.php.

Related

See also index of Function Reference and index of Template Tags.