2014-10-28 67 views
1

我使用https://github.com/WebDevStudios/CMB2,使現場組對我的產品頁面,這裏提供一個metabox自定義字段組,圖像字段值不顯示在前端

function cmb2_sample_metaboxes(array $meta_boxes) { 
 

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

 

 

 
\t 
 

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

 
\t // Add other metaboxes as needed 
 

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

這個問題只與圖像領域它沒有顯示在前端,當我檢查chroome上的元素沒有img標籤,<?php echo $title; ?>工作正常,並顯示在前端,所以我是想知道我在做什麼錯了形象。

回答

2

解決了,我發現我做錯了,圖像ID應根據文檔product_image_id。必須在您爲該字段設置的圖像ID末尾添加「_id」。