WordPress.org

Codex

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

Function Reference/get category by path

Description

Retrieve category based on URL containing the category slug.

Breaks the $category_path parameter up to get the category slug.

Tries to find the child path and will return it. If it doesn't find a match, then it will return the first category matching slug, if $full_match, is set to false. If it does not, then it will return null.

It is also possible that it will return a WP_Error object on failure. Check for it when using this function.

Usage

<?php get_category_by_path$category_path$full_match$output ?>

Parameters

$category_path
(string) (required) URL containing category slugs.
Default: None
$full_match
(boolean) (optional) Whether should match full path or not.
Default: true
$output
(string) (optional) Constant OBJECT, ARRAY_A, or ARRAY_N
Default: OBJECT

Return Values

(null|object|array) 
Null on failure. Type is based on $output value.

Examples

Default usage

<?php
$categ = get_category_by_path('uncategorized');
echo "Category ".$categ->name;
?>

Notes

Change Log

Since: 2.1.0

Source File

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

Related

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