php - Get the id of a WooCommerce category, by name -
i have wp template assign pages. template ie. display woocommerce products have same master category name pages name itself.
by far have tried using code, no output:
$idobj = get_category_by_slug($pagename); $id = $idobj->term_id; echo ": ". $id;
unfortunately, echo not display anything.
echoing $pagename
works, , returns me slug of page.
any way make work?
with custom taxonomy recommended use get_term_by()
instead :
$category = get_term_by( 'slug', $pagename, 'product_cat' ); $cat_id = $category->term_id
reference: get category id category slug…
Comments
Post a Comment