Images are note being displayed from Custom Table

Support MB Custom Table Images are note being displayed from Custom TableResolved

Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
    Posts
  • #38840
    Md. Wasim FirozMd. Wasim Firoz
    Participant

    Hello, 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

    #38849
    Long NguyenLong Nguyen
    Moderator

    Hi,

    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 function get_term_meta() only helps you to get the field value from the default table termmeta.

    #38864
    Md. Wasim FirozMd. Wasim Firoz
    Participant

    Hello 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 🙂

    #38868
    Long NguyenLong Nguyen
    Moderator

    Hi,

    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'] . '">';
    }
    #38871
    Md. Wasim FirozMd. Wasim Firoz
    Participant

    Hello, 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

    #38874
    Long NguyenLong Nguyen
    Moderator

    Hi,

    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

    #38875
    Md. Wasim FirozMd. Wasim Firoz
    Participant

    Hello 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

    #38877
    Long NguyenLong Nguyen
    Moderator

    Hi,

    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.

    #38878
    Md. Wasim FirozMd. Wasim Firoz
    Participant

    Hello, 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.

    #38884
    Long NguyenLong Nguyen
    Moderator

    Hi,

    Please share your site credentials via this contact form https://metabox.io/contact/
    I will help you to check the issue.

    #38890
    Md. Wasim FirozMd. Wasim Firoz
    Participant

    Hello, I've submitted the login details via the given contact form link. Thank you

    #38897
    Long NguyenLong Nguyen
    Moderator

    Hi,

    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' ) );
    #38952
    Md. Wasim FirozMd. Wasim Firoz
    Participant

    Hello 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.

    #38957
    PeterPeter
    Moderator

    Hello 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 );
Viewing 14 posts - 1 through 14 (of 14 total)
  • You must be logged in to reply to this topic.