Get taxonomy slug by ID

The function gets taxonomy slug by taxonomy ID in WordPress. For example to get product category slug in WooCommerce: $slug = wpcb_get_taxonomy_slug_by_id( $id, 'product_cat' );

if ( ! function_exists( 'wpcb_get_taxonomy_slug_by_id' ) ) {
	/**
	 * wpcb_get_taxonomy_slug_by_id.
	 *
	 * @see https://wpcodebook.com/snippets/get-taxonomy-slug-by-id/
	 */
	function wpcb_get_taxonomy_slug_by_id( $id, $taxonomy ) {
		return ( ( $term = get_term_by( 'id', $id, $taxonomy ) ) ? $term->slug : false );
	}
}

No comments yet.

Leave a Reply