0

so I'm fairly new to worpdress development and I'm still learning my way through, I tried searching anywhere but I cannot find the answer I'm looking for.

The image below is the All categories from my Custom Post Type named Jewelry, How can I display this categories in my website? I tried using get_categories() but it doesn't work that way or I'm using it the wrong way

enter image description here

The code below just returns all categories from my POST not my CUSTOM POST TYPE

$categories = get_categories(array(
   'hide_empty' => false,
));

1 Answer 1

1

If you know the taxonomy slug of it you can use code below:

$tax_slug = 'product_cat';
$cpt_terms = get_term( $tax_slug );
echo '<pre>';
  print_r( $terms );
echo '</pre>';

but if you don't know the slug, you can use these method: First click on the taxonomy you want to see its terms. In the URL bar in your browser you find sth like this:

http://localhost/wordpress/wp-admin/edit-tags.php?taxonomy=product_cat&post_type=product

you can find taxonomy slug and use it in the above code.

Second use code below to find taxonomies of a Post Type:

$cpt_tax = get_object_taxonomies( array( 'post_type ) => 'product' );
echo '<pre>';
  print_r( $cpt_tax);
echo '</pre>';

Note: In this example I used 'product' as post type slug. if you don't know the slug you can find it by clicking on all posts of your CPT and in the URL bar of your browser you find this:

http://localhost/wordpress/wp-admin/edit.php?post_type=product

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.