Home › Forums › Add-ons › Flexible Content Field › get_post_meta for a repeater field in a flexible content field? › Reply To: get_post_meta for a repeater field in a flexible content field?
Hi James,
I have a flexible content field called “menu” (it’s a Restaurant menu), which has a repeater field, where each repeater field has a title. I want each title of this section as a dropdown menu for the corresponding page in the menu.
See below my export of the particular field:
array (
'key' => '586a0fb66fdef',
'name' => 'menukaart-layout',
'label' => 'Menukaart layout',
'display' => 'block',
'sub_fields' => array (
array (
'default_value' => 0,
'message' => '',
'ui' => 1,
'ui_on_text' => '',
'ui_off_text' => '',
'key' => 'field_586f538fcd098',
'label' => 'Toon menukaart menu',
'name' => 'toon_menukaart_menu',
'type' => 'true_false',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
),
array (
'sub_fields' => array (
array (
'default_value' => '',
'maxlength' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'key' => 'field_586a0fe56fdf1',
'label' => 'Categorie',
'name' => 'categorie',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
),
array (
'return_format' => 'array',
'preview_size' => 'thumbnail',
'library' => 'all',
'min_width' => '',
'min_height' => '',
'min_size' => '',
'max_width' => '',
'max_height' => '',
'max_size' => '',
'mime_types' => '',
'key' => 'field_586a0fea6fdf2',
'label' => 'Afbeelding',
'name' => 'afbeelding',
'type' => 'image',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
),
array (
'default_value' => '',
'new_lines' => '',
'maxlength' => '',
'placeholder' => '',
'rows' => '',
'key' => 'field_586a0ff56fdf3',
'label' => 'Omschrijving',
'name' => 'omschrijving',
'type' => 'textarea',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
),
array (
'sub_fields' => array (
array (
'default_value' => '',
'maxlength' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'key' => 'field_586a10056fdf5',
'label' => 'Naam',
'name' => 'naam',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
),
array (
'default_value' => '',
'maxlength' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'key' => 'field_586a10106fdf6',
'label' => 'Prijs',
'name' => 'prijs',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
),
array (
'default_value' => '',
'maxlength' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'key' => 'field_586a101b6fdf7',
'label' => 'Omschrijving',
'name' => 'omschrijving',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
),
),
'min' => 0,
'max' => 0,
'layout' => 'row',
'button_label' => 'Nieuw menu-item',
'collapsed' => '',
'key' => 'field_586a0ffc6fdf4',
'label' => 'Menu-item',
'name' => 'menu-item',
'type' => 'repeater',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
),
),
'min' => 0,
'max' => 0,
'layout' => 'row',
'button_label' => 'Nieuwe categorie',
'collapsed' => '',
'key' => 'field_586a0fbe6fdf0',
'label' => 'Menukaart',
'name' => 'menukaart-repeater',
'type' => 'repeater',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
),
array (
'clone' => array (
0 => 'group_5874c031b6839',
),
'prefix_label' => 0,
'prefix_name' => 0,
'display' => 'seamless',
'layout' => 'block',
'key' => 'field_5874c0d52c82f',
'label' => 'Informatie',
'name' => 'informatie',
'type' => 'clone',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
),
),
'min' => '',
'max' => '',
)
I’d want the “Categorie” sub_field in my menu. I’m using a custom walker, the entire code can be seen below. It works, as long as the repeater field is the 2nd field in my flexible content.
class Cusom_Menu extends Walker_Nav_Menu {
static $count=0;
function start_el(&$output, $item, $depth, $args) {
global $wp_query;
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
$class_names = $value = '';
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
$class_names = ' class="page-item-'. $item->object_id .' ' . esc_attr( $class_names ) . '"';
if ($depth==0) self::$count=0; // reset var when we are in first level
if ($depth==1 && self::$count==1) { // if we are in submenu and items count is 1...
$output .= '<li class="terug mobile-only"><a href="#"><i class="fa fa-lg fa-chevron-left" aria-hidden="true"></i> Terug</a></li>';
$parent = $item->menu_item_parent;
$output .= '<li class="mobile-only"><a class="hoofd-item" href="#">hoi' . $parent->ID .'</a></li></a></li>';
}
$output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';
$attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : '';
$attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : '';
$attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : '';
$attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : '';
$parent = wp_get_post_parent_id($item->object_id);
$thumbnail = '';
if ( has_post_thumbnail( $item->object_id ) and 1 == $depth ) {
$thumbnail = get_the_post_thumbnail( $item->object_id );
}
$item_output = $args->before;
if (1 == $depth and $thumbnail and $parent == 43){
$item_output .= '<a'. $attributes .' class="hoofdlink">';
$item_output .= '<div class="afbeelding">' . $thumbnail . '</div><span>';
} else {
$item_output .= '<a'. $attributes .' class="link-'. $depth .'">';
}
$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
if (1 == $depth and $thumbnail and $parent == 43){
$item_output .= '</span>';
}
$item_output .= '</a>';
$repeater_value = get_post_meta($item->object_id, 'flexibele_content_1_menukaart-repeater', true);
if (1 == $depth and $repeater_value and $parent == 43){
for ($i=0; $i<$repeater_value; $i++) {
$meta_key = 'flexibele_content_1_menukaart-repeater_'.$i.'_categorie';
$meta_key = 'flexibele_content_1_menukaart-repeater_'.$i.'_categorie';
$sub_field_value = get_post_meta($item->object_id, $meta_key, true);
$sub_field_link = (str_replace(' ', '-', strtolower($sub_field_value)));
$item_output .= '<div><a class="sublink" href="' . esc_attr( $item->url ) .'#'. $sub_field_link .'">';
$item_output .= $sub_field_value;
$item_output .= '</a></div>';
}
}
$item_output .= $args->after;
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
self::$count++;
}
function start_lvl( &$output, $depth = 0, $args = array() ) {
$indent = str_repeat("\t", $depth);
$output .= "\n$indent<ul class='sub-menu'><div class='wrapper clearfix'>\n";
}
function end_lvl( &$output, $depth = 0, $args = array() ) {
$indent = str_repeat("\t", $depth);
$output .= "$indent</div></ul>\n";
}
}
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.