Can't get terms' meta values in frontend
Support › MB Term Meta › Can't get terms' meta values in frontend
- This topic has 4 replies, 2 voices, and was last updated 9 years ago by
academyadmin.
-
AuthorPosts
-
April 5, 2016 at 6:50 AM #2671
academyadmin
ParticipantHello,
I bought the full bundle of your plugins and have encountered a difficulty with the term meta one.
I installed the metabox plugin, then the extension, registered the term meta for category featured image just like in the example you provide.
I then created a new category and uploaded a picture. On the admin side it all looks OK.
Now, in order to query the terms I created a new page called 'Categories' and used a customized template called 'Categories' for it.I'm using the most recent versions of both wordpress and metabox.io.
The php for my page looks like this:
<?php /* Template name: Categories */ ?> <?php get_header(); $cats = get_terms('category'); foreach ($cats as $cat) { var_dump($cat); echo '<br><br>'; echo $cat->name; //yields correct category name echo '<br><br>'; $meta = get_term_meta( $cat->$term_id, 'image_advanced', false ); var_dump($meta); //yields FALSE echo '<br><br>'; $meta2 = get_term_meta( $cat->$term_id, 'color', true ); var_dump($meta2); //yields FALSE echo '<br><br>'; $a = get_metadata( 'category', $cat->$term_id, 'image_advanced', false ); var_dump($a); //yields FALSE echo '<br><br>'; echo rwmb_meta( 'image_advanced', $args, $cat->$term_id ); echo rwmb_meta( 'color', $args, $cat->$term_id ); $images = rwmb_meta( 'image_advanced'); // Since 4.8.0 //$images = rwmb_meta( 'image_advanced', 'type=image_advanced' ); // Prior to 4.8.0 if ( !empty( $images ) ) { foreach ( $images as $image ) { echo "<a href='{$image['full_url']}' rel='lightbox'><img src='{$image['url']}' width='{$image['width']}' height='{$image['height']}' alt='{$image['alt']}' /></a>"; } } } // Restore original Post Data wp_reset_postdata(); ?> <?php get_sidebar(); ?> <?php get_footer(); ?>
As you can see, I tried a few options there...
All I get for the dumps of the meta is FALSE.Please advise - how do I properly show the image/color in the front end?
This is my metabox registration code:
add_action( 'rwmb_meta_boxes', 'prefix_register_taxonomy_meta_boxes' ); function prefix_register_taxonomy_meta_boxes( $meta_boxes ) { $meta_boxes[] = array( 'title' => 'Standard Fields', 'taxonomies' => 'category', // List of taxonomies. Array or string 'fields' => array( array( 'name' => __( 'Featured Image', 'prefix' ), 'id' => 'image_advanced', 'type' => 'image_advanced', ), array( 'name' => __( 'Color', 'prefix' ), 'id' => 'color', 'type' => 'color', ), ), ); return $meta_boxes; }
April 7, 2016 at 2:05 PM #2692Anh Tran
KeymasterHi, sorry for taking long to reply :(.
Your code is almost correct. It's just not
$cat->$term_id
, but$cat->term_id
. So the code inside the loop should be:$images = get_term_meta( $cat->term_id, 'image_advanced', false ); var_dump( $images ); // Array of image IDs $color = get_term_meta( $cat->term_id, 'color', true ); var_dump( $color ); // Color
April 7, 2016 at 2:43 PM #2693academyadmin
ParticipantHello,
thank you. I think I might still need your help in the thread:
this is the code (comments show the problem):<?php /* Template name: Categories */ ?> <?php get_header(); $categories = get_categories(); foreach ($categories as $cat) { echo $cat->name; //yields category name echo '<br><br>'; $images = get_term_meta( $cat->term_id, 'image_advanced', false ); //yields an array of strings, i.e. "4" echo '<br><br>'; if ( $images ) { foreach ( $images as $image ) { echo "<a href='{$image['full_url']}' rel='lightbox'><img src='{$image['url']}' width='{$image['width']}' height='{$image['height']}' alt='{$image['alt']}' /></a>"; /* this echo line yields: Warning: Illegal string offset 'full_url' in categories.php on line 18 Warning: Illegal string offset 'url' in categories.php on line 18 Warning: Illegal string offset 'width' in categories.php on line 18 Warning: Illegal string offset 'height' in categories.php on line 18 Warning: Illegal string offset 'alt' in categories.php on line 18 */ } } $images2 = rwmb_meta( 'image_advanced'); //this method returns an empty string, why? var_dump($images2); } // Restore original Post Data wp_reset_postdata(); ?> <?php get_sidebar(); ?> <?php get_footer(); ?>
Please advise...
Thanks!April 7, 2016 at 4:09 PM #2695Anh Tran
KeymasterThe thing here is the values return from the function
get_term_meta
is an array of images' IDs. It doesn't return the full info of images like inrwmb_meta
. To get full image info, you can use this code:foreach ( $images as $image ) { $image = RWMB_Image_Field::file_info( $image, 'thumbnail' ); echo "<a href='{$image['full_url']}' rel='lightbox'><img src='{$image['url']}' width='{$image['width']}' height='{$image['height']}' alt='{$image['alt']}' /></a>"; }
April 7, 2016 at 5:26 PM #2698academyadmin
ParticipantThanks a lot, it works like a charm.
-
AuthorPosts
- The topic ‘Can't get terms' meta values in frontend’ is closed to new replies.