Support Forum
Support › MB Term Meta › Can't get image to output
//Add metaboxes to Sector taxonomy ///////////////////////////////////////////////////////
add_filter( 'rwmb_meta_boxes', 'prefix_register_taxonomy_meta_boxes' );
function prefix_register_taxonomy_meta_boxes( $meta_boxes ){
$meta_boxes[] = array(
'title' => 'Extra info',
'taxonomies' => 'base_sector', // List of taxonomies. Array or string
'fields' => array(
// featured image
array(
'name' => 'Featured Image',
'id' => 'tax_img',
'type' => 'image_advanced',
),
),
);
return $meta_boxes;
}
In my template I'm trying to output the image but can't figure out how to do it.
<?php // Get Sectors
$post_type = 'base_project';
// Get all the taxonomies for this post type
$taxonomies = get_object_taxonomies( array( 'post_type' => $post_type ) );
foreach( $taxonomies as $taxonomy ) :
// Gets every term in this taxonomy
$terms = get_terms( $taxonomy );
foreach( $terms as $term ) :
echo "<div class='grid-item'><div class='pic sector'>";
$images = get_term_meta( $term->term_id, 'image_advanced', false );
if ( $tax_images ) {
foreach ( $tax_images as $tax_image ) {
$tax_image = RWMB_Image_Field::file_info( $tax_image, 'thumbnail' );
echo "<img src='{$tax_image['url']}' alt='{$tax_image['alt']}' />";
}
}
?>
taking from the example in the documentation
// Getting images
$image_ids = get_term_meta( $term_id, $field_id, false ); // Media fields are always multiple.
foreach ( $image_ids as $image_id ) {
$image = RWMB_Image_Field::file_info( $image_id, array( 'size' => 'thumbnail' ) );
echo '<img src="' . $image['url'] . '">';
}
I've setup the metaboxes like this
add_filter( 'rwmb_meta_boxes', 'prefix_register_taxonomy_meta_boxes' );
function prefix_register_taxonomy_meta_boxes( $meta_boxes ){
$meta_boxes[] = array(
'title' => 'Extra info',
'taxonomies' => 'base_sector', // List of taxonomies. Array or string
'fields' => array(
// featured image
array(
'name' => 'Featured Image',
'id' => 'tax_img',
'type' => 'image_advanced',
),
),
);
return $meta_boxes;
}
I've been playing around with it for hours and can't get it to work.
Hi there,
Can you use var_dump( $image_ids )
and give me the output?
Thanks.
Managed to sort it. I wasn't query the terms properly.
<?php $custom_terms = get_terms('base_sector');
foreach($custom_terms as $custom_term) {
$term_id = $custom_term->term_id;
$image_ids = get_term_meta( $term_id, 'tax_img', false );
foreach ( $image_ids as $image_id ) {
$image = RWMB_Image_Field::file_info( $image_id, array( 'size' => 'thumbnail' ) );
echo "<img src='{$image['url']}' alt='{$image['alt']}' />";
}
}
?>