I have a question that I can’t seem to find the right approach to.

I’m currently developing a WordPress site whereby a custom submenu is required. In said submenu, a list of category terms is displayed (incidentally, a list of years – 2003-2017). For each of these years, I have created an image field using the brilliant Advanced Custom Fields PRO plugin. The idea is that an image can be uploaded for a year in the “Edit Category” page. So far, so good. This image would then be shown in the submenu alongside the year, and here’s where I am stumped.

What I am having trouble with is figuring out how I am going to check for that field in the submenu and fetch it.

I’ll include the code for the custom nav walker below. Any help would be greatly appreciated!

class Nav_Header_Walker extends Walker_Nav_Menu {

    public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) {
        if ( ! $element ) {
            return;
        }

        $id_field = $this->db_fields['id'];
        $id = $element->$id_field;

        // Display this element.
        $this->has_children = ! empty( $children_elements[ $id ] );
        if ( isset( $args[0] ) && is_array( $args[0] ) ) {
            $args[0]['has_children'] = $this->has_children; // Backwards compatibility.
        }

        $cb_args = array_merge( array( &$output, $element, $depth ), $args );
        call_user_func_array( array( $this, 'start_el' ), $cb_args );

        // Descend only when the depth is right and there are children for this element.
        if ( ( 0 === $max_depth || $max_depth > $depth + 1 ) && isset( $children_elements[ $id ] ) ) {

            foreach ( $children_elements[ $id ] as $child ) {

                if ( ! isset( $newlevel ) ) {
                    $newlevel = true;
                    // Start the child delimiter.
                    $cb_args = array_merge( array( &$output, $depth ), $args );

                    /** Additional check for custom addition of id to sub-level */
                    if ( $element->post_name = 'Megatron' ) {
                        $cb_args['sub_menu_id'] = 'megatron';
                    }
                    /** End custom check */

                    call_user_func_array( array( $this, 'start_lvl' ), $cb_args );
                }
                $this->display_element( $child, $children_elements, $max_depth, $depth + 1, $args, $output );
            }
            unset( $children_elements[ $id ] );
        }

        if ( isset( $newlevel ) && $newlevel ) {
            // End the child delimiter.
            $cb_args = array_merge( array( &$output, $depth ), $args );
            call_user_func_array( array( $this, 'end_lvl' ), $cb_args );
        }

        // End this element.
        $cb_args = array_merge( array( &$output, $element, $depth ), $args );
        call_user_func_array( array( $this, 'end_el' ), $cb_args );
    }

    public function start_lvl( &$output, $depth = 0, $args = array(), $sub_menu_div = null ) {
        $indent = str_repeat( "t", $depth );
        if ( $sub_menu_div ) {
            $output .= "n$indent<div id="$sub_menu_div"><ul class="sub-menu">n";
        } else {
            $output .= "n$indent<ul class="sub-menu">n";
        }
    }

    function end_lvl( &$output, $depth = 0, $args = array() ) {
        $indent = str_repeat( "t", $depth );
        $output .= "$indent</ul></div>n";
    }
}

Read more here: ACF PRO taxonomy term field in WordPress submenu


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: