admin管理员组

文章数量:1122832

I am using a Repeater of Advance custom field for the content of the my additional custom WooCommerce Tab. The repeater is inside a group field. I manage to display the custom fields that is outside the repeater field. Now the problem is the field inside my repeater field. The repeater field is not displaying. Here is the code I used in my functions.php

// Add a Custom 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;
}

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>';
        }
}

I am using a Repeater of Advance custom field for the content of the my additional custom WooCommerce Tab. The repeater is inside a group field. I manage to display the custom fields that is outside the repeater field. Now the problem is the field inside my repeater field. The repeater field is not displaying. Here is the code I used in my functions.php

// Add a Custom 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;
}

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>';
        }
}
Share Improve this question edited Nov 18, 2017 at 8:28 John Ashley Nohay asked Nov 18, 2017 at 8:22 John Ashley NohayJohn Ashley Nohay 12 bronze badges 5
  • 1 is designer_achievements a field or a subfield? – CK MacLeod Commented Nov 18, 2017 at 8:40
  • can you show us a screenshot on how you created the field/subfiled ? – Temani Afif Commented Nov 18, 2017 at 8:51
  • designer_achievement is a repeater type field then achievement is the subfield. Here is how I made the field. ibb.co/mGzvv6 – John Ashley Nohay Commented Nov 18, 2017 at 10:19
  • @CKMacLeod here is the screenshot – John Ashley Nohay Commented Nov 18, 2017 at 12:01
  • @TemaniAfif here is the screenshot – John Ashley Nohay Commented Nov 18, 2017 at 12:01
Add a comment  | 

3 Answers 3

Reset to default 0

I believe you need to use get_sub_field() for the repeater field (designer_achievements) and get_field() for its parent. Please take a look at the ACF documentation for the Repeater field's subfield.

You will also need to use a have_rows() loop, as noted in the documentation.

if( have_rows('parent_field') ):

    while( have_rows('parent_field') ) : the_row();

        $value = get_sub_field('sub_field');

    endwhile;

endif;

replace $achievements = get_field('designer_achievements'); to $achievements = get_sub_field('designer_achievements');

I search to do that and finally found a solution.You can do something like that:

This implies that your products have a repeater field "tabs" that contains a subfield "title" and a subfield "content".

add_filter( 'woocommerce_product_tabs', 'prefix_other_products_tab' );
function prefix_other_products_tab( $tabs ) {
    global $product;
    $additional_tabs = get_field('tabs', $product->get_id());
    foreach($additional_tabs as $index => $custom_tab){
        $slug = sanitize_title($custom_tab['title']);
        $tabs[$slug+$index] = array(
            'title' => $custom_tab['title'],
            'priority'  => 120,
            'content' => $custom_tab['content'],
            'callback'  => function($tab_name, $tab){
                global $product;
                echo $tab['content'];
            },
        );
    }
    return $tabs;
}

本文标签: phpWooCommerce Custom Tab with ACF Repeater Field