Taxonomy_MetaData icon indicating copy to clipboard operation
Taxonomy_MetaData copied to clipboard

get method term field value in loop on frontend

Open reesmcivor opened this issue 10 years ago • 14 comments

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>';

reesmcivor avatar Apr 14 '14 06:04 reesmcivor

What do you get when you:

echo '<xmp>: '. print_r( Taxonomy_MetaData::get( 'product_cat', $cat->term_id ), true ) .'</xmp>';

jtsternberg avatar Apr 14 '14 14:04 jtsternberg

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.

reesmcivor avatar Apr 15 '14 04:04 reesmcivor

Okay I am also getting same issue. On a loop, I get same metadata value though everything is okay in backend..

sachittandukar avatar Apr 19 '14 07:04 sachittandukar

Fixed in 3b8e9d9139931851b0fc6a8c670363a67a6b63f7. Those reporting the bug, please update and test.

jtsternberg avatar May 29 '14 03:05 jtsternberg

I think there's still issue. I am getting empty value on get method.

sachittandukar avatar Jun 01 '14 06:06 sachittandukar

I am also getting an empty value using:

echo '<xmp>: '. print_r( Taxonomy_MetaData::get( 'curriculum_areas', 96), true ) .'</xmp>';

sb-lc avatar Jan 06 '15 10:01 sb-lc

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 avatar Jan 06 '15 10:01 sb-lc

@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' );

jtsternberg avatar Jan 06 '15 19:01 jtsternberg

unfortunately, this didn't work, it outputs nothing just like calling it without the 3rd parameter

sb-lc avatar Jan 07 '15 09:01 sb-lc

the public function get_meta() in taxonomy_metadata_cmb2.php returns nothing. the check for class CMB2 returns false.

sb-lc avatar Jan 07 '15 09:01 sb-lc

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.

jtsternberg avatar Jan 07 '15 14:01 jtsternberg

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

sb-lc avatar Jan 07 '15 14:01 sb-lc

When are you calling the getter function? I'm curious why you would need taxonomy meta values before init.

jtsternberg avatar Jan 07 '15 15:01 jtsternberg

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!

mimomedia avatar Aug 13 '15 08:08 mimomedia