wpmetabox / meta-box

The best plugin for WordPress custom fields and custom meta boxes
https://metabox.io
1.19k stars 423 forks source link

How do you display an image on the front end using a cloned group? #864

Closed mike-church closed 8 years ago

mike-church commented 8 years ago

In advanced, I thank you for this plugin. It's been a life saver.

I've got a metabox group that supports a text field, image advanced field, and a wysiwyg. This group is can be cloned. I can get and display all the values on the front end except for the image field. I've searched all day through the support site, documentation, and google and can't find a solution. I hope you can help. This is the code I'm using to display on the front end:

<?php
$my_group = rwmb_meta( 'my_group_id' );
foreach ( $my_group as $group_value ) {
$images = isset( $group_value['my_image_id'] ) ? $group_value['my_image_id'] : '';
$text = isset( $group_value['my_text_id'] ) ? $group_value['my_text_id'] : '';
$wysiwyg = isset( $group_value['my_wysiwyg_id'] ) ? $group_value['my_wysiwyg_id'] : '';
?>
<?php foreach ( $images as $image ) { echo "<img src='{$image['url']}' />"; } ?>
<?php echo $text;?>
<?php echo $wysiwyg;?>
<p>Other stuff I'm outputing.</p>
<?php
}
?>

The resulting image output code looks like this <img src="3">

rilwis commented 8 years ago

Thanks for your question.

The helper function doesn't return full info for images in a group as it does for normal fields. We have to do a little work for that. Here is how to get more info about the image:

<?php
$my_group = rwmb_meta( 'my_group_id' );
foreach ( $my_group as $group_value ) {
    $images = isset( $group_value['my_image_id'] ) ? $group_value['my_image_id'] : array();
    $text = isset( $group_value['my_text_id'] ) ? $group_value['my_text_id'] : '';
    $wysiwyg = isset( $group_value['my_wysiwyg_id'] ) ? $group_value['my_wysiwyg_id'] : '';
    foreach ( $images as $image ) {
        $image = RWMB_Image_Field::file_info( $image, array( 'size' => 'thumbnail' ) );
        // print_r( $image );
        echo "<img src='{$image['url']}' />";
    }
    ?>
    <?php echo $text;?>
    <?php echo $wysiwyg;?>
    <p>Other stuff I'm outputing.</p>
    <?php
}
?>

I'll update the documentation to make this clear.
mike-church commented 8 years ago

Works perfectly. Thank you so much!

Giacomo92 commented 6 years ago

Same problem here @rilwis. I have the last version of metabox plugin and I follow the doc. Here the code for register the metabox:

array(
          'id' => 'gallery-images',
      'name' => __('gallery-images','stm_vehicles_listing'),
      'type' => 'image_advanced',
           'force_delete' => false,
           'max_file_uploads' => 10,
           'max_status' => 'false',
       'image_size' => 'thumbnail',
       'tab' => 'photosandvideos',
),

I register the metabox with: add_filter( 'rwmb_meta_boxes', 'prefix_register_meta_boxes' );

When I try to get the value on frontend.i get only ID of the first image. I have already tried all this alternative from the referenced isuees:

$post_id = get_the_ID(); // int(1264) --> The post ID is not empty

$gallery = rwmb_meta('gallery-images'); //string(4) "1530" -> The id of the first image 
$gallery = rwmb_meta('gallery-images','size=full',$post_id); //string(4) "1530" -> The id of the first image 
$gallery = rwmb_meta('gallery-images','size=full'); //string(4) "1530" -> The id of the first image 
$gallery = rwmb_meta('gallery-images','size=full',$post_id); //string(4) "1530" -> The id of the first image 
$gallery = rwmb_meta('gallery-images','size=thumbnail'); //string(4) "1530" -> The id of the first image 
$gallery = rwmb_meta('gallery-images'); //string(4) "1530" -> The id of the first image 
$gallery = rwmb_meta('gallery-images',array( 'size' => 'full' ),$post_id); //string(4) "1530" -> The id of the first image 
$gallery = rwmb_meta('gallery-images',array( 'size' => 'full' )); //string(4) "1530" -> The id of the first image 
$gallery = rwmb_meta('gallery-images',array( 'size' => 'thumbnail' ),$post_id); //string(4) "1530" -> The id of the first image 
$gallery = rwmb_meta('gallery-images',array( 'size' => 'thumbnail' )); //string(4) "1530" -> The id of the first image 

On the backend I set and I can see my three image. Why i can get an array of image from $gallery?

rilwis commented 6 years ago

Hi @Giacomo92 , let's talk about the problem on the new issue #1235 that you've just created.

maxxdesign-vc commented 2 months ago

Hello I used the code as above to retrieve the image in a cloned group but I get alle the 3 images of the group (by now) not the each one assingned to the author (in this case)

<?php $extras = rwmb_meta( 'extra' ) ?: []; ?>
        <?php $link = get_the_permalink(); ?>
        <?php foreach ( $extras as $extra ) { 
        echo '<div class="card">';?>
        <?php
        $my_group = rwmb_meta( 'extra' );
        foreach ( $my_group as $group_value ) {
        $images = isset( $group_value['author_pict'] ) ? $group_value['author_pict'] : array();
        foreach ( $images as $image ) {
        $image = RWMB_Image_Field::file_info( $image, array( 'size' => 'thumbnail' ) );
        echo "<img src='{$image['url']}' />";
        }
        ?>
        <?php
        }
        ?> 
        <?php echo '<h5 class="book-author"><a href="' .$link. '">' .$extra['author_name'] .'&nbsp;' .$extra['author_surname']. '</a></h5>'; ?>
        <?php echo '</div>';
        }
        ?>

Here the HTML issue: https://www.maxxdesign.it/eddegrenelle/auteurs-extra

rilwis commented 2 months ago

@maxxdesign-vc I guess you double get the extra group data, in the $extras and $my_group. So, there are 2 loops. I don't know how you register fields, but here is a "fix" for the code above:

<?php $extras = rwmb_meta( 'extra' ) ?: []; ?>
<?php $link = get_the_permalink(); ?>
<?php foreach ( $extras as $extra ) { 
    echo '<div class="card">';?>
    <?php
    $images = isset( $extra['author_pict'] ) ? $extra['author_pict'] : array();
    foreach ( $images as $image ) {
        $image = RWMB_Image_Field::file_info( $image, array( 'size' => 'thumbnail' ) );
        echo "<img src='{$image['url']}' />";
    }
    ?>
    <?php echo '<h5 class="book-author"><a href="' .$link. '">' .$extra['author_name'] .'&nbsp;' .$extra['author_surname']. '</a></h5>'; ?>
    <?php echo '</div>';
}
?>