1
votes

I use wordpress CMS and CMB2 plugin to create cmb fields.It is perfect combination but I can't get alt text from my images added using "file" field type. As usual I register repeatable fields and bringing them to frontend by this :

<?php $successful_students_items = get_post_meta( get_the_id(), 'successful_students_block_box', true );
                                  if( !empty( $successful_students_items ) ) {
                                    foreach( $successful_students_items as $successful_students_item ) { ?>
                                <div class="ipd-je-8 ipd-rz-8 ipd-pv-12 ipd-ke-24">
                                    <div class="rjeudirnskxzi nnu">
                                                        <div class="h1ge6swchqzj6de">
                                                            <div class="h1ge6swchqzj6de_cvz">
                                                          <figure class="o309iruhgtybgyu5">
                                          <blockquote><?php echo $successful_students_item['successful_students_block_testimonial']; ?>
                                            <div class="ingi3h38d8jj4"></div>
                                          </blockquote>
                                          <img alt="" src="<?php echo $successful_students_item['successful_students_block_image']; ?>"/>
                                          <div class="ewr4i9548jfdeuthie">
                                            <h5><?php echo $successful_students_item['successful_students_block_name_lastname']; ?>
                                              <span>
                                                <br>
                                                <?php echo $successful_students_item['successful_students_block_position']; ?>
                                              </span>
                                            </h5>
                                          </div>
                                        </figure>
                                                          </div>
                                                        </div>
                                                      </div>
                                                  </div>
                                    <?php } 
                                  } ?>

but I can't get image alt text , tried to google but nothing of what I've found work.. Maybe anyone can help? Thanks!!

2

2 Answers

0
votes

CMB always store an ID for the images at the time uploading that. You just need to grab that in following way :

$successful_students_block_image_id = get_post_meta( get_the_id(), $successful_students_item['successful_students_block_image_id'], true );
$gallery_image=wp_get_attachment_image( $successful_students_block_image_id,'full');
echo $gallery_image;

this will get you the alt for that image and print the image.

-1
votes

You can achieve Your requirement by bit of alternative way, means by using the file_list type of CMB2 you will get the array of images. So use the array_key to get the individual image id e.g

foreach( array_keys($successful_students_items) as $successful_students_item_id ) {

/** Now by using this id you can get the individual data of an attachment**/

$successful_students_block_image_alt = get_post_meta( $successful_students_item_id, '_wp_attachment_image_alt', true ); $successful_students_block_image_url_details = wp_get_attachment_image_src( $successful_students_item_id, 'full');

/** this will return an array **/ $successful_students_block_image_url = $successful_students_block_image_url_details[0];

} /** Now you can use this individual data in img tag.**/