ACF object in wordpress menu-walker

I have a WP site and I want a submenu for each item, with li-item for each the_sub_field('info_block_header')

. So I'm trying to create a walker with an ACF loop inside, however I don't understand how to properly implement the loop. This is what I got and it turned out badly:

sidebar.php:

<aside>
    <nav id="mainNav">

                <?php 
                $dropdown = new dropdown_walker;

                    $mainNav = array(
                   'theme_location'  => '',
                   'menu'            => 'main-menu', 
                   'container'       => 'ul', 
                   'container_class' => 'topmenu-{topmenu slug}-container', 
                   'container_id'    => 'topmenu',
                   'menu_class'      => 'topmenu', 
                   'menu_id'         => 'topmenu-{topmenu slug}[-{increment}]',
                   'echo'            => true,
                   'fallback_cb'     => 'wp_page_menu',
                   'before'          => '',
                   'after'           => '',
                   'link_before'     => '<h4>',
                   'link_after'      => '</h4>',
                   'items_wrap'      => '<ul id="%1$s" class="%2$s">%3$s</ul>',
                   'depth'           => 0,
                   'walker'          => $dropdown
                  );

                wp_nav_menu( $mainNav ); 


                ?>

                    <?php get_search_form(); ?>
</aside>

      

page.php

<?php get_header(); ?>
<?php get_sidebar(); ?>
<?php if(get_field('info_block')): ?>
                <?php while(has_sub_field('info_block')): ?>
                <div class="box masonCh boxText col2 clearfix">
                    <h2><?php the_sub_field('info_block_header')?></h2>
                    <div>
                        <?php the_sub_field('info_block_txt')?>
                    </div>

                </div>
                <?php if ( have_rows('info_block_img')):
                    while (have_rows ('info_block_img')) : the_row();?>
                <div class="box masonCh boxImg col2 clearfix">
                    <div class="boxCrop">
                            <img src="<?php the_sub_field('info_block_img_file') ?>">
                    </div>
                </div>

      

dropdown_menu.php

<?php 
class dropdown_walker extends Walker {

    var $tree_type = array( 'post_type', 'taxonomy', 'custom' );

    var $db_fields = array( 'parent' => 'menu_item_parent', 'id' => 'db_id' );

    function start_lvl( &$output, $depth = 0, $args = array() ) {
        $indent = str_repeat("\t", $depth);
        $output .= "\n$indent<ul class=\"sub-menu\">\n";
    }
    function end_lvl( &$output, $depth = 0, $args = array() ) {
        $indent = str_repeat("\t", $depth);
        $output .= "$indent</ul>\n";
    }

    function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
        $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';

        $class_names = $value = '';

        $classes = empty( $item->classes ) ? array() : (array) $item->classes;
        $classes[] = 'menu-item-' . $item->ID;

        $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
        $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';

        $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
        $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';

        $output .= $indent . '<li' . $id . $value . $class_names .'>';

        $atts = array();
        $atts['title']  = ! empty( $item->attr_title ) ? $item->attr_title : '';
        $atts['target'] = ! empty( $item->target )     ? $item->target     : '';
        $atts['rel']    = ! empty( $item->xfn )        ? $item->xfn        : '';
        $atts['href']   = ! empty( $item->url )        ? $item->url        : '';


        $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args );

        $attributes = '';
        foreach ( $atts as $attr => $value ) {
            if ( ! empty( $value ) ) {
                $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
                $attributes .= ' ' . $attr . '="' . $value . '"';
            }
        }

        $item_output = $args->before;
        $item_output .= '<a'. $attributes .'>';

        $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
        $item_output .= $args->after;
        $item_output .= '</a>';


        $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );

        //Here’s where I’m trying to put my submenu:

        $output .= '<ul id="about" class="dropDown">'. if(get_field('info_block')):
                while(has_sub_field('info_block')): .'
        <li><a href="#">'. get_sub_field('info_block_header') .'</a></li>'. endwhile; endif; .
        </ul>'; 

    }   function end_el( &$output, $item, $depth = 0, $args = array() ) {
        $output .= "</li>\n";
    }

} // Walker_Nav_Menu
?>

      

+3


source to share


1 answer


You need to pass the id associated with it: you can get it in $ item-> ID, you can find out the type of this print_r object (type $ item->); (taxonomy, page, etc.).

Then:



$output .= '<ul id="about" class="dropDown">'. if(get_field('info_block', $id)):
            while(has_sub_field('info_block', $id)): .'
    <li><a href="#">'. get_sub_field('info_block_header', $id) .'</a></li>'. endwhile; endif; .
    </ul>'; 

      

0


source







All Articles