0
votes

I use WordPress with CMB2 plugin to create custom meta boxes and fields.

It works perfectly but I can't get file field type image alt in the loop.

I have repeatable field type file successful_students_block_image in meta box called successful_students_block.

My code looks like this. It works but I don't know how to get image alt text.

<?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
    }
  } ?>

and this is what i have on backend in functions.php

function successful_students_blocks_register_metabox() {
$prefix = 'successful_students_block_';  
  $cmb = new_cmb2_box( array(
        'id'            => $prefix . 'metabox',
        'title'         => esc_html__( 'Successful Students', 'cmb2' ),
        'object_types'  => array( 'page', ), 
        'show_on'       => array( 'key' => 'page-template', 'value' => 'fo4if93/tpl-successful-students.php' ),
    ) );

  $group_field_id = $cmb->add_field( array(
    'id'            => $prefix . 'box',
    'type'          => 'group',
    'repeatable'    => true, 
    'options'       => array(
    'group_title'   => __( 'Student {#}', 'cmb2' ),
    'add_button'    => __( 'Add Another Student', 'cmb2' ),
    'remove_button' => __( 'Remove Student', 'cmb2' ),
    'sortable'      => true, 
    ),
  ) );

  $cmb->add_group_field( $group_field_id, array(
    'name'          => 'Name Lastname',
    'id'            => $prefix . 'name_lastname',
    'type'          => 'text',
  ) );


  $cmb->add_group_field( $group_field_id, array(
    'name'          => 'Image',
    'id'            =>  $prefix . 'image',
    'type'          => 'file',
    'options'       => array(
    'url'           => false, 
    ),
    'text'    => array(
    'add_upload_file_text' => 'Add File'
    ),
  ) );

}
add_action( 'cmb2_init', 'successful_students_blocks_register_metabox' );

Trying to solve this problem several days. Please anybody if know will be happy for any answer^^

2
How do you define your image field?yivi
you mean ID of custom field of image? this is the image field id on cmb2 backend (successful_students_block_image)nito
No. I mean: at some point you define your cmb2 metaboxes and fields. Post the field/metabox definition (in your question).yivi

2 Answers

0
votes

I'm not realy sure, but I think you can access the image ID (inside your foreach loop) with this:

$image_id = $successful_students_item['successful_students_block_image_id'];

From the CMB2 Wiki:

A file uploader. By default it will store the file url and allow either attachments or URLs. This field type will also store the attachment ID (useful for getting different image sizes). It will store it in $id . '_id', so if your field id is wiki_test_image the ID is stored in wiki_test_image_id.

With the image ID you can get all available data that belongs to. i.e.:

$image_alt = get_post_meta( $image_id, '_wp_attachment_image_alt', true);

Your code can look like this:

<?php
$image_id = $successful_students_item['successful_students_block_image_id'];
$image_alt = get_post_meta( $image_id, '_wp_attachment_image_alt', true);
?>

<img alt="<?php echo esc_attr( $image_alt ); ?>" src="<?php echo $successful_students_item['successful_students_block_image']; ?>"/>
0
votes

I think you need to try this.

$alt_text = wp_get_attachment_metadata( $image_id, true ) ;
echo  $alt_text['sizes']['thumbnail']['file']; 

For the further info on wp_get_attachment_metadata()

All the best.