将ACF图像添加到Walker菜单

时间:2019-07-30 20:40:19

标签: wordpress

之后,我试图将ACF图像添加到Walker大型菜单,但无法在Walker功能中找到关闭的位置。 因此,图片应位于ul之后的此处,并位于'megamenu-panel div

$output .= "\n" . $indent . ' <div class="megamenu-panel"> <div class="megamenu-lists"><ul class="' . $class_names . '" data-slick-index="slide-idx" data-hover="dropdown" data-animations="zoomIn zoomIn zoomIn zoomIn">' . "\n";

也许我可以不使用ACF而以其他方式添加图像,但是我想到的第一件事就是对菜单使用ACF图像

我的Walker功能是:

<?php
// OW Front-end Nav Walker
class megamenu_group_secnav_walker extends Walker_Nav_Menu {

    function display_element($element, &$children_elements, $max_depth, $depth=0, $args, &$output) {

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

        // add custom class and carets to menu has children
        if(in_array("menu-item-has-children", $element->classes)) {
            $element->classes[] = 'dropdown';
            $element->title .= ' <i class="fa fa-angle-down"></i>';
        }

        // add class active
        if(in_array("current-menu-item", $element->classes)) {
            $element->classes[] = 'active';
        }

        parent::display_element($element, $children_elements, $max_depth, $depth, $args, $output);
    }


    /**
     * Starts the list before the elements are added.
     *
     * Adds classes to the unordered list sub-menus.
     *
     * @param string $output Passed by reference. Used to append additional content.
     * @param int    $depth  Depth of menu item. Used for padding.
     * @param array  $args   An array of arguments. @see wp_nav_menu()
     */
    function start_lvl( &$output, $depth = 0, $args = array() ) {
        // Depth-dependent classes.
        $indent = ( $depth > 0  ? str_repeat( "\t", $depth ) : '' ); // code indent
        $display_depth = ( $depth + 1); // because it counts the first submenu as 0
        $classes = array(
            '',
            ( $display_depth % 2  ? 'menu-odd' : 'menu-even' ),
            ( $display_depth >=2 ? '' : '' ),
            'menu-depth-' . $display_depth
        );
        $class_names = implode( ' ', $classes );

        // Build HTML for output.
        $output .= "\n" . $indent . ' <div class="megamenu-panel"> <div class="megamenu-lists"><ul class="' . $class_names . '" data-slick-index="slide-idx" data-hover="dropdown" data-animations="zoomIn zoomIn zoomIn zoomIn">' . "\n";
    }


    /**
     * Start the element output.
     *
     * Adds main/sub-classes to the list items and links.
     *
     * @param string $output Passed by reference. Used to append additional content.
     * @param object $item   Menu item data object.
     * @param int    $depth  Depth of menu item. Used for padding.
     * @param array  $args   An array of arguments. @see wp_nav_menu()
     * @param int    $id     Current item ID.
     */
    function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
        global $wp_query;
        $indent = ( $depth > 0 ? str_repeat( "\t", $depth ) : '' ); // code indent

        // Depth-dependent classes.
        $depth_classes = array(
            ( $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ),
            ( $depth >=2 ? 'sub-sub-menu-item' : '' ),
            ( $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ),
            'menu-item-depth-' . $depth
        );
        $depth_class_names = esc_attr( implode( ' ', $depth_classes ) );

        // Passed classes.
        $classes = empty( $item->classes ) ? array() : (array) $item->classes;
        $class_names = esc_attr( implode( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) ) );

        // Build HTML.
        $output .= $indent . '<li class="megamenu-list list-col-4">';

        // Link attributes.
        $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        ) .'"' : '';
        $attributes .= ' class="menu-link ' . ( $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ) . '"';
        $attributes .= ' href12="dsfsdfsfd"';

        // Build HTML output and pass through the proper filter.
        $item_output = sprintf( '%1$s<a%2$s>%3$s%4$s%5$s</a>%6$s',
            $args->before,
            $attributes,
            $args->link_before,
            apply_filters( 'the_title', $item->title, $item->ID ),
            $args->link_after,
            $args->after
        );
        $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
    }


}

header.php中的调用函数

<?php
    $megamenu_group_walkermega = new megamenu_group_secnav_walker();
    if ( has_nav_menu('primary') ):
                                                    $megamenu_group_megamenu = wp_nav_menu(
   array(
                                                            'theme_location' => 'primary',
  'depth'=> 8,
                                                            'container'=>'ul',
  'menu_class' => 'nav-menu align-to-right',
                                                            'fallback_cb'    => '',
 'walker' => $megamenu_group_walkermega,
 'echo' => false
 )
 );
echo wp_kses_post($megamenu_group_megamenu);
else:?>
  <?php endif;
 ?>

子菜单中的图像应如下所示: https://ibb.co/dKTmksz

0 个答案:

没有答案