自定义字段组,图像字段值未显示在前端

时间:2014-10-28 17:23:24

标签: php wordpress

我正在使用https://github.com/WebDevStudios/CMB2为我的产品页面创建一个字段组,这是元数据库的代码



function cmb2_sample_metaboxes( array $meta_boxes ) {

	// Start with an underscore to hide fields from custom fields list
	$prefix = '_cmb2_';



	

	/**
	 * Repeatable Field Groups
	 */
	$meta_boxes['field_group'] = array(
		'id'           => 'field_group',
		'title'        => __( 'Manage your products here', 'cmb2' ),
		'object_types' => array( 'page', ),
		'fields'       => array(
			array(
				'id'          => $prefix . 'repeat_group',
				'type'        => 'group',
				'options'     => array(
					'group_title'   => __( 'Product {#}', 'cmb2' ), // {#} gets replaced by row number
					'add_button'    => __( 'Add Another Product', 'cmb2' ),
					'remove_button' => __( 'Remove Product', 'cmb2' ),
					'sortable'      => true, // beta
				),
				'fields'      => array(
					array(
						'name' => 'Product Name',
						'id'   => 'product_name',
						'type' => 'text',
					),
					array(
						'name' => 'Product Description',
						'description' => 'Write a short description for this Product',
						'id'   => 'product_description',
						'type' => 'textarea_small',
					),
					array(
						'name' => 'Product Image',
						'id'   => 'product_image',
						'type' => 'file',
					),
					array(
						'name' => __( 'Image Caption', 'cmb2' ),
						'desc' => __( 'Write a short description for the image', 'cmb2' ),
						'id'   => 'image_caption',
						'type' => 'text_medium',
					),
				),
			),
		),
	);

	// Add other metaboxes as needed

	return $meta_boxes;
}




我想在下面的html标记上显示它



<div class="container products">
        <div class="row">
          <?php

            $products = get_post_meta( get_the_ID(), '_cmb2_repeat_group', true );

            foreach ( (array) $products as $key => $product ) {

                $img = $title = $caption = '';

                if ( isset( $product['product_name'] ) )
                    $title = esc_html( $product['product_name'] );


                if ( isset( $product['product_image'] ) ) {            
                    $img = wp_get_attachment_image( $product['product_image'], 'thumbnail', null, array(
                        'class' => 'img-thumbnail',
                    ) );
                }
                $caption = isset( $product['image_caption'] ) ? wpautop( $product['image_caption'] ) : '';

                // Do something with the data
            

          ?>
          <div class="col-xs-5 col-sm-4 col-sm-offset-1 box">
            <a href="#">
              <?php echo $img; ?>
              <span class="rotate-caption hidden-xs">
                <h3><?php echo $title; ?></h3>
                <hr>
                <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis,</p>
              </span>
            </a>
          </div>
 
          <?php } ?>
&#13;
&#13;
&#13;

问题只在于它没有在前端显示的图像字段,当我检查chroome上的元素时没有img标记,<?php echo $title; ?>工作正常并显示在前端,所以我想知道我对图像做错了什么。

1 个答案:

答案 0 :(得分:2)

解决了,我发现我做错了,根据文档,图片ID应该是product_image_id。必须在您为该字段设置的图片ID的末尾添加“_id”。