我使用带有 CMB2 插件的 WordPress 来创建自定义元框和字段。
它工作得很好,但我无法在循环中获取文件字段类型图像 alt。
我successful_students_block_image
在元框中有可重复的字段类型文件,称为successful_students_block
.
我的代码看起来像这样。它有效,但我不知道如何获取图像替代文本。
<?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
}
} ?>
这就是我在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' );
试图解决这个问题几天。请任何知道的人都会很高兴得到任何答案^^