+14 votes
5.6k views
in Programming by (11.7k points)
edited by
I am developing a project and want to learn how to check whether category exists or not. How can I check it?

1 Answer

+5 votes
by (10.4k points)

Firstly create a php file in root folder of wordpress directory

Include wp-load.php ->

<?php include "wp-load.php"; ?>

and you can check with this code (slug search)

$category_slug = "home";

if ($term = get_term_by('slug', $category_slug, 'category'))
{
$term_ID = $term->term_id;
$cat_id = $term_ID;
}
else $cat_id=-1;

OR by name

 

if ( is_term( 'uncategorized' , 'category' /* do not change this */ ) ) // category_slug, search_place
	return true;
else
	return false;

OR by id

$cat = get_category(1); // cat_ID
if ( !empty( $cat ) )
	return true;
by (1.2k points)
Thank you very much
Ask a Question
Welcome to WikiTechSolutions where you can ask questions and receive answers from other members of the community.

You can ask a question without registration.

Categories

...