Images are note being displayed from Custom Table
Support › MB Custom Table › Images are note being displayed from Custom TableResolved
- This topic has 13 replies, 3 voices, and was last updated 2 years, 6 months ago by
Peter.
-
AuthorPosts
-
October 27, 2022 at 1:46 AM #38840
Md. Wasim Firoz
ParticipantHello, How are you doing? I need your help.
Could you please check out why the following code is not working to display images from the custom filed? and custom field data is being inserted into a custom table.
<div> // 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'] . '" />'; } </div>
Where do I need to make the changes to make it work? Thank you
October 27, 2022 at 3:00 PM #38849Long Nguyen
ModeratorHi,
To get the field value in the custom table, please follow the documentation to use the helper function
rwmb_meta()
https://docs.metabox.io/extensions/mb-custom-table/#getting-field-value
the WordPress functionget_term_meta()
only helps you to get the field value from the default tabletermmeta
.October 28, 2022 at 2:03 AM #38864Md. Wasim Firoz
ParticipantHello brother, Thank you for getting back to me. I need your help. Let me explain you the scenario here -
First of all I've gone through multiple documents including your given link but I couldn't make it work.
I have a taxonomy for the author. I've created 1 custom field which is displayed in the author's taxonomy.
This custom field is for uploading a single image and saved in a custom database. The author taxonomy data will be selected from any woocommerce product.
On the front-end on any woocommerce product page under a tab I want to display this image if that product has that taxonomy selected. I've tried both following codes for retrieving image but it;s not working
<?php $price = rwmb_meta( 'price', ['storage_type' => 'custom_table', 'table' => 'properties'], 15 ) ?> <p> <strong>Property price:</strong> <?= number_format( $price ) ?> USD </p>
$args = [ 'storage_type' => 'custom_table', 'table' => $table_name, ]; $value = rwmb_meta( $field_id, $args, $post_id ); echo $value;
By the way, this is working for me when I am saving the custom field data into the default database.
function woo_author_details_tab_content() { global $product; $taxonomy = 'book-author'; // <== Here set your custom taxonomy if( ! taxonomy_exists( $taxonomy ) ) return; // exit $term_ids = wp_get_post_terms( $product->get_id(), $taxonomy, array('fields' => 'ids') ); foreach($term_ids as $terms) { $term_id = $terms; } if ( ! empty($term_ids) ) { $image=""; foreach($term_ids as $terms) { $term_id = $terms; $image_ids = get_term_meta( $term_id, 'author_image_db', false ); foreach ( $image_ids as $image_id ) { $image = RWMB_Image_Field::file_info( $image_id, array( 'size' => 'thumbnail' ) ); echo '<img style="border-radius: 10px;" src="' . $image['url'] . '"><br />'; } } } }
Please help me to retrieve the image and display it from the custom table. I badly need this help. Thank you in advance 🙂
October 28, 2022 at 10:44 AM #38868Long Nguyen
ModeratorHi,
Here is an example code to get the term meta from the custom table
$args = [ 'storage_type' => 'custom_table', 'table' => 'my_table_name', 'object_type' => 'term' // here ]; $value = rwmb_meta( 'field_id', $args, $term_id ); echo $value;
As I said before, the WP function
get_term_meta()
will not work in this case.foreach( $term_ids as $term_id ) { $args = [ 'storage_type' => 'custom_table', 'table' => 'properties', // table name 'object_type' => 'term' // require to add this argument, see here https://docs.metabox.io/extensions/mb-term-meta/#getting-field-value ]; $image_id = rwmb_meta( 'author_image_db', $args, $term_id ); // if the field is single_image, no need to use the for loop $image = RWMB_Image_Field::file_info( $image_id, array( 'size' => 'thumbnail' ) ); echo '<img src="' . $image['url'] . '">'; }
October 28, 2022 at 2:03 PM #38871Md. Wasim Firoz
ParticipantHello, Thank you for getting back to me.
I tried the following code and tested out a few other things but nothing did the work for me. Need your help
function woocommerce_author_single_image() { global $product; $taxonomy = 'book-author'; if( ! taxonomy_exists( $taxonomy ) ) return; // exit $term_ids = wp_get_post_terms( $product->get_id(), $taxonomy, array('fields' => 'ids') ); foreach( $term_ids as $term_id ) { $args = [ 'storage_type' => 'custom_table', 'table' => 'author_image_db', // table name 'object_type' => 'term' // require to add this argument, see here https://docs.metabox.io/extensions/mb-term-meta/#getting-field-value ]; $image_id = rwmb_meta( 'author_single_image', $args, $term_id ); // if the field is single_image, no need to use the for loop $image = RWMB_Image_Field::file_info( $image_id, array( 'size' => 'thumbnail' ) ); echo '<img src="' . $image['url'] . '">'; } }
Could you please help me to fix it? It's holding me back for the next steps. Thank you
October 28, 2022 at 6:10 PM #38874Long Nguyen
ModeratorHi,
Can you please share the code that creates the custom fields on your site? Refer to this solution https://docs.metabox.io/extensions/meta-box-builder/#getting-php-code
October 28, 2022 at 7:48 PM #38875Md. Wasim Firoz
ParticipantHello Long, I exported the code of the custom field. Please check this out -
<?php add_filter( 'rwmb_meta_boxes', 'your_prefix_function_name' ); function your_prefix_function_name( $meta_boxes ) { $prefix = ''; $meta_boxes[] = [ 'title' => __( 'Author Image', 'your-text-domain' ), 'id' => 'author-image', 'taxonomies' => ['book-author'], 'storage_type' => 'custom_table', 'table' => 'author_single_image_db', 'fields' => [ [ 'name' => __( 'Single Image', 'your-text-domain' ), 'id' => $prefix . 'author_single_image', 'type' => 'single_image', 'label_description' => __( 'Author Image', 'your-text-domain' ), 'desc' => __( 'Upload Your Image', 'your-text-domain' ), 'admin_columns' => [ 'position' => 'after name', 'sort' => true, 'searchable' => true, 'filterable' => true, ], 'columns' => 2, ], ], ]; return $meta_boxes; }
Thank you
October 28, 2022 at 10:40 PM #38877Long Nguyen
ModeratorHi,
So you can see the table name in the argument is not correct, please change it to
$args = [ 'storage_type' => 'custom_table', 'table' => 'author_single_image_db', // table name 'object_type' => 'term' ];
Let me know how it goes.
October 29, 2022 at 1:43 AM #38878Md. Wasim Firoz
ParticipantHello, When I gave you my code at that time the field and database name were the same - author_single_image.
So I switched the table name to 'author_single_image_db', which existed in my code when writing this message to you. But sadly it's not working.
I am placing down my code again for your review -
function woocommerce_author_single_image() { global $product; $taxonomy = 'book-author'; if( ! taxonomy_exists( $taxonomy ) ) return; // exit $term_ids = wp_get_post_terms( $product->get_id(), $taxonomy, array('fields' => 'ids') ); foreach( $term_ids as $term_id ) { $args = [ 'storage_type' => 'custom_table', 'table' => 'author_single_image_db', // table name 'object_type' => 'term' // require to add this argument, see here https://docs.metabox.io/extensions/mb-term-meta/#getting-field-value ]; $image_id = rwmb_meta( 'author_single_image', $args, $term_id ); // if the field is single_image, no need to use the for loop $image = RWMB_Image_Field::file_info( $image_id, array( 'size' => 'thumbnail' ) ); echo '<h4>test</h4>'; echo '<img src="' . $image['URL'] . '">'; } }
Here is the screenshot of the code - https://share.getcloudapp.com/6quG4B60
On the frontend the word "test" is printed but not the image.
If necessary I can give you access to the site. Please get back to me when you get a chance. Thank you.
October 29, 2022 at 12:14 PM #38884Long Nguyen
ModeratorHi,
Please share your site credentials via this contact form https://metabox.io/contact/
I will help you to check the issue.October 29, 2022 at 6:25 PM #38890Md. Wasim Firoz
ParticipantHello, I've submitted the login details via the given contact form link. Thank you
October 30, 2022 at 2:26 PM #38897Long Nguyen
ModeratorHi,
I've fixed the code on your site, the single image displays as well on the product page. Just remove the code
// if the field is single_image, no need to use the for loop $image = RWMB_Image_Field::file_info( $image_id, array( 'size' => 'thumbnail' ) );
November 4, 2022 at 12:57 AM #38952Md. Wasim Firoz
ParticipantHello Long, Thank you so much for your great support.
I need one more help with the image.
How can I show the image on the term meta archive page? I want to display the author's image on the Author's archive page. The author name and Author description are automatically displayed but not the image which is a custom field.
Please point me to the guide or tell me the location, hook or file name where I need to add the code to retrieve the image data. Thank you.
November 4, 2022 at 10:02 PM #38957Peter
ModeratorHello Md Wasim,
I will take care of this topic from now. On the archive page, you can use the WordPress function get_queried_object_id() to get the term ID and pass it to the helper function
rwmb_meta()
as you do above.$term_id = get_queried_object_id() $image_id = rwmb_meta( 'author_single_image', $args, $term_id );
-
AuthorPosts
- You must be logged in to reply to this topic.