Thanks, it took a bit of puzzling but I figured it out using your links.
Sorry, reading it back now I agree it isn’t really clear what I’m trying to accomplish 😉
I have added a two custom fields to attachments (media) using ACF.
Whenever I choose a featured image in a post, I want the custom fields for this particular attachment to show below the featured image, and save them when saving the post.
Ugh, I was in the assumption that that wouldn’t work in a walker, but I just tried a simple custom field and it works without problems.
I’ll try to fix it this way, thanks! 🙂
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";
}
}
Hi James,
Thank you. I figured out it saved its data like this. The thing I’m struggling with is automatically finding the correct number. This is the code I currently have:
$repeater_value = get_post_meta($item->object_id, 'flexibele_content_1_menukaart-repeater', true);
if (1 == $depth and $repeater_value){
for ($i=0; $i<$repeater_value; $i++) {
$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 .= '<a class="sublink" href="' . esc_attr( $item->url ) .'#'. $sub_field_link .'">';
$item_output .= $sub_field_value;
$item_output .= '</a>';
}
}
I can’t believe it was that easy. Thanks!
Actually, I found out what went wrong. I had another meta_query which overruled the one sent above.
Consider this question as solved.
That’s genius. Thanks so much!
Hi,
I actually solved it by making an array and then check for duplicate custom fields, like so:
<?php
$args = array(
'post_type' => 'deals'
);
$the_query = new WP_Query($args);
$unieke_deals = array();
if ($the_query->have_posts()):
while ($the_query->have_posts()):
$the_query->the_post();
$bedrijf = get_field('company');
if (!in_array($bedrijf, $unieke_deals)):
$unieke_deals[] = $bedrijf;
?>
<?php the_field('company'); ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br />
<?php
endif;
endwhile;
endif;
?>
It’s a row from the Flexible Content! 🙂
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.