0

I have an ecommerce with about 30 categories. Every category has a custom taxonomy (for ex. brands) with a lot of entries.

I need to get every custom taxonomy entry for a single woocommerce category.

Example. Category smartphone Category tablet Category smartwatch

Every category has the "brand" custom taxonomy.

So the function i need is something like

function get_brands($category_id) {
$brand_name = function_get_brandname();
$brand_url = function_get_brandurl();

$final_array[] = $brand_name => $brand_url;
}

For now i'm just trying to get the term_list. This is my simple code:

$my_id_getter = $this->args['ids']; //this get the product id    
$mytest = get_the_term_list( $my_id_getter, 'brands', '', ', ' );

This code works, the problem is that i get only brands associated with the product, and not every brand in the category. I've tried to put the category-id instead of the product-id, but is not working, it returns a null array.

Thank you in advance. -Kryuko

  • What have you already tried? Show us your current code that you have problems with... – Krzysiek Dróżdż Mar 30 '18 at 14:41
3

There's a bit of a difference between Taxonomies and Terms.

  • Taxonomy refers to a collection. For example, Category is the Post Taxonomy and inside this taxonomy you can have many Terms which can be assigned to the post. For more information check out The Codex, it may explain the difference better.
  • Term also refers to a collection but a collection of similar items. In the example above we are using the Category and inside that we could have News where all our News Posts are collected and assigned to.

Based on your question I'm going to assume when you say "taxonomy" you really mean "Term in the Brand Taxonomy". The easiest approach if you want a list of assigned brands is to use get_the_term_list().

get_the_term_list( $post_id, 'brands' );

The above will display all the brands of a specific post in a comma separated list.


If you want to grab all terms you could use get_terms():

$brand_arr = get_terms( array(
    'taxonomy'      => 'brands',
    'hide_empty'    => false,
) );

if( ! is_wp_error( $brand_arr ) ) {

    // Loop through array of values ( foreach )
    // $term->name will display the name
    // Use `get_term_link()` to generate the term URL

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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