Taxonomy_MetaData
Taxonomy_MetaData copied to clipboard
get method term field value in loop on frontend
I am trying to get woocommerce category's on the front-end along with color field I have setup with Customer Meta Box field type. Works well in the admin area with my chosen color. But in a loop on the frontend the same color value comes out:
Taxonomy_MetaData::get( 'product_cat', $cat->term_id, 'color');
$item_output .= '<ul id="shop-menu">';
$cats = get_terms( 'product_cat', $args );
foreach($cats as $cat) {
$color = Taxonomy_MetaData::get( 'product_cat', $cat->term_id, 'color' );
$item_output .= '<li><div class="tile" data-term-id="' . $cat->term_id . '" style="color:'.$color.'">' . $cat->name . '</li>';
}
$item_output .= '</ul>';
What do you get when you:
echo '<xmp>: '. print_r( Taxonomy_MetaData::get( 'product_cat', $cat->term_id ), true ) .'</xmp>';
Comes back with an array key of color but the values are the same. Also that might be of help, the value only appears as many times as I have set it for.
Okay I am also getting same issue. On a loop, I get same metadata value though everything is okay in backend..
Fixed in 3b8e9d9139931851b0fc6a8c670363a67a6b63f7. Those reporting the bug, please update and test.
I think there's still issue. I am getting empty value on get method.
I am also getting an empty value using:
echo '<xmp>
: '. print_r( Taxonomy_MetaData::get( 'curriculum_areas', 96), true ) .'</xmp>
';
I am using cmb2, but this also returns an empty value :
echo '<xmp>: '. print_r( Taxonomy_MetaData_CMB2::get( 'curriculum_areas', 96), true ) .'</xmp>';
the parameters are definite correct
@sb-lc the get method for the CMB2 abstraction did not work the same way but I have fixed it. But it's unusual and unlikely that you would want to use it in that form. You'd be better off passing the third parameter which is the key of the data you want. For example:
$sub_title = Taxonomy_MetaData_CMB2::get( 'curriculum_areas', 96, 'sub_title' );
unfortunately, this didn't work, it outputs nothing just like calling it without the 3rd parameter
the public function get_meta() in taxonomy_metadata_cmb2.php returns nothing. the check for class CMB2 returns false.
Sounds like you haven't actually included CMB2 then. You can see in the example that that is the first thing that should happen. Alternatively you can install/activate the plugin: https://wordpress.org/plugins/cmb2.
thanks for the reply, it seems that CMB2/init.php doesn't include the class early enough because its priority is set to 9999, therefore it is not available for use at the point where taxonomy_metadata_CMB2 needs it, hence the public function get_meta returns false where it checks if cmb2 class exists. i am trying to work around this by using get_post_meta to retrieve the tax meta values
When are you calling the getter function? I'm curious why you would need taxonomy meta values before init.
Hi, congrats for this great class, i am trying to use it in Woocommerce product categories to set a icon select field for each category.
The field shows, saves the value and all seems to work properly. But i cannot get the saved values.
I am inside a woocommerce products loop and i am using:
$mm_tax_icon = Taxonomy_MetaData_CMB2::get( 'product_cat', $single_cat->term_id, 'mm_tax_icon' );
when i do var_dump($mm_tax_icon) i get 'bool(false)'
I have included the CMB2 class and just the line before for example
$meta_coords = get_post_meta( get_the_ID(), 'mm_location', true );
works properly to retrieve a value saved for products in a cmb2 metabox, so i think cmb2 has been initiated. Also i have tested if(class_exists()) looking for cmb2 and it returns true.
Thank you very much!