2017-11-18 122 views
1

我正在使用高級自定義字段的Repeater作爲我的其他自定義WooCommerce選項卡的內容。轉發器在組字段中。在WooCommerce自定義產品中使用高級自定義字段Repeater字段選項卡

我設法顯示中繼器字段外的自定義字段。

這是我在functions.php使用的代碼:

add_filter('woocommerce_product_tabs', 'dl_custom_product_designer_tab'); 
function dl_custom_product_designer_tab($tabs) { 
    // ensure ACF is available 
    if (!function_exists('have_rows')) 
     return; 

    if (get_field('designer')) { 
     $tabs[] = array(
      'title' => 'DESIGNER', 
      'priority' => 50, 
      'callback' => 'dl_custom_designer_tab' 
     ); 
    } 
    return $tabs; 
} 

function dl_custom_designer_tab() { 
    $designer = get_field('designer'); 
     echo '<p>'.$designer['designer_image'].'</p>'; 
     echo '<p>'.$designer['designer_name'].'</p>'; 
     echo '<p>'.$designer['designer_short_description'].'</p>'; 
     // loop through the rows of data 
     $achievements = get_field('designer_achievements'); 
     if($achievements) { 
      // loop through the rows of data 
      echo '<ul>'; 
      foreach($achievements as $achievement){ 
       // display a sub field value 
       echo '<li>'.$achievement['achievement'].'</li>'; 
      } 
      echo '</ul>'; 
     } 
} 

現在的問題是我的中繼器領域內場:中繼子字段沒有顯示任何。

我做錯了什麼?我如何獲得中繼器子字段的輸出?


編輯:A screenshot of the ACF settings repeater field

+0

repeater字段返回你一個數組,所以如果你只有一個在repeater字段中的行,它應該是'$ designer [0] ['designer_image']'等等......如果你有更多的你應該使用一個循環... – Shibi

+0

@JohnAshleyNohay我已經更新了我的答案。 – LoicTheAztec

回答

0

UPDATE(新功能替代)

顯然,這不作品有「產品」後型號... **這看起來就像這個插件的錯誤(我已經能夠以測試相同的情況並重現該問題)

它應該被報告給作者的支持行事 ......我已經在我身邊做了。


一個臨時的解決(只要這個錯誤不是由ACF團隊解決)

這是自定義的功能替換爲ACF中繼專用功能:

/** 
* Custom function: Get an array of ACF repeater sub-field. 
* 
* @param string $master_field (the 
* @param string $repeater_field 
* @param array $sub_fields 
* @output formatted html 
*/ 

function repeater_subfield($group_name, $repeater, $subfield){ 
    global $post, $product; 

    $repeater_meta_key = $group_name.'_'.$repeater; 
    $rows = get_post_meta($post->ID, $repeater_meta_key, true); 
    for($i = 0; $i < $rows; $i++){ 
     $subfield_meta_key = $repeater_meta_key.'_'.$i.'_'.$subfield; 
     $output[] = get_post_meta($post->ID, $subfield_meta_key, true); 
    } 
    if(count($rows) > 0) return $output; 
    else return; 
} 

然後您的測試和功能代碼應該是:

// Add a custom product tab 
add_filter('woocommerce_product_tabs', 'dl_custom_product_designer_tab'); 
function dl_custom_product_designer_tab($tabs) { 
    // ensure ACF is available 
    if (!function_exists('have_rows')) 
     return; 

    if (get_field('designer')) { 
     $tabs[] = array(
      'title' => 'DESIGNER', 
      'priority' => 50, 
      'callback' => 'dl_custom_designer_tab' 
     ); 
    } 
    return $tabs; 
} 

// The custom product tab content 
function dl_custom_designer_tab() { 
    global $post, $product; 

    $group_name = 'designer'; 

    $designer = get_field($group_name); 

    echo '<p>'.$designer['designer_image'].'</p>'; 
    echo '<p>'.$designer['designer_name'].'</p>'; 
    echo '<p>'.$designer['designer_short_description'].'</p>'; 

    $designer_achievements = repeater_subfield($group_name, 'designer_achievements', 'achievement'); 

    // check if the repeater field has rows of data 
    if(count($designer_achievements) > 0): 
     echo '<ul>'; 

     // loop through the rows of data 
     foreach($designer_achievements as $achievement){ 
      // display a sub field value 
      echo '<li>'.$achievement.'</li>'; 
     } 

     echo '<ul>'; 
    else: 

     // "no rows found" optional message 
     echo '<p><em>No data…</em></p>'; 

    endif; 
} 

代碼會出現在您的活動子主題(或主題)的function.php文件中,或者也存在於任何插件文件中。

測試和工程...


原來的答覆:

隨着Advanced Custom Field Pro的插件來獲得直放站子字段中的數據,您將需要使用the documented way,函數和方法have_rows()get_sub_field(),這樣:

function dl_custom_designer_tab() { 
    global $post; 

    $designer = get_field('designer'); 

    echo '<p>'.$designer['designer_image'].'</p>'; 
    echo '<p>'.$designer['designer_name'].'</p>'; 
    echo '<p>'.$designer['designer_short_description'].'</p>'; 

    // check if the repeater field has rows of data 
    if(have_rows('designer_achievements')): 
     echo '<ul>'; 
     // loop through the rows of data 
     while (have_rows('designer_achievements')) : the_row(); 
      // display a sub field value 
      echo '<li>' . get_sub_field('achievement') . '</li>'; 
     endwhile; 

     echo '<ul>'; 
    else: 

     // "no rows found" optional message 
     echo '<p><em>No row founds in repeater…</em></p>'; 

    endif; 
} 
+0

已經嘗試過那個。仍然沒有工作。它只是顯示其他回顯而不是包含內容的行。無論如何感謝您的幫助,這裏是我如何創建字段的截圖。 https://ibb.co/mGzvv6 –

+0

@JohnAshleyNohay我找到了一種可行的方法......我也自己複製了你的問題,我認爲這是與ACF Pro中產品帖子類型和轉發器功能相關的插件中的一個錯誤。我已經用一個可行的替代方案更新了我的答案...嘗試一下並讓我知道。謝謝 – LoicTheAztec

相關問題