Support

Account

Home Forums ACF PRO Repeater fields in menu

Unread

Repeater fields in menu

  • I’d like to add page specific custom CTAs in the main menu of my site. I have created a Default CTA field and a Custom CTA repeater field. In the custom CTA repeater field the user can select the custom CTA (link field) and the pages (multiple post objects) on which this custom CTA will show. On the rest of the pages, the Default CTA will show.

    I am following the ACF documentation for adding fields to WordPress Menus. But when I try to loop through the Custom CTA repeater field, I am getting these errors:

    <b>Notice</b>:  Array to string conversion in <b>/Users/prashansapoddar/documents/wp-sites/realease/wp-content/plugins/advanced-custom-fields-pro/includes/api/api-template.php</b> on line <b>357</b><br />
    <br />
    <b>Notice</b>:  Array to string conversion in <b>/Users/prashansapoddar/documents/wp-sites/realease/wp-content/plugins/advanced-custom-fields-pro/includes/api/api-value.php</b> on line <b>42</b><br />

    So far, my code is this:

    add_filter('wp_nav_menu_items', 'acf_wp_nav_menu_ctas', 10, 2);
    
    function acf_wp_nav_menu_ctas( $items, $args ) {
    
      // get menu
      $menu = wp_get_nav_menu_object($args->menu);
    
      // modify primary only
      if( $args->theme_location == 'primary-menu' ) {
    
        // default CTA vars
        $default_cta = get_field('nav_cta', $menu);
        $default_cta_link = $default_cta['url'];
        $default_cta_text = $default_cta['title'];
    
        // custom CTA vars
    
        $custom_ctas = get_field('custom_ctas', $menu);
    
        if( have_rows( $custom_ctas ) ) {
    
          while( have_rows( $custom_ctas )) : the_row();
    
          $selected_page = get_sub_field('selection');
    
            if ( $selected_page ) {
    
              foreach($selected_page as $post_object) {
    
                $custom_cta = get_sub_field('cta', $menu);
                $custom_cta_link = $custom_cta['url'];
                $custom_cta_text = $custom_cta['title'];
    
                if(is_page($post_object->ID))
    
                $nav_cta = '<li class="site__header-cta"><a href="'.$custom_cta_link.'" class="button button--secondary">'.$custom_cta_text.'</a></li>';
    
                // append html
                $items = $items . $nav_cta;
              }
            }
    
          endwhile;
    
        } else {
    
          $nav_cta = '<li class="site__header-cta"><a href="'.$default_cta_link.'" class="button button--secondary">'.$default_cta_text.'</a></li>';
    
          // append html
          $items = $items . $nav_cta;
    
        }
      }
      // return
      return $items;
    }
    

    Any help would be much appreciated. I would also like some guidance on how I could check for the page selected for the custom CTA (should I use WP’s is_page() functionality)?

    Thanks

Viewing 1 post (of 1 total)

The topic ‘Repeater fields in menu’ is closed to new replies.