0

我有一个问题,我似乎无法找到正确的方法。

我目前正在开发一个需要自定义子菜单的 WordPress 网站。在所述子菜单中,显示类别术语列表(顺便说一下,年份列表 - 2003-2017)。这些年来,我都使用出色的 Advanced Custom Fields PRO 插件创建了一个图像字段。这个想法是可以在“编辑类别”页面中上传一年的图像。到目前为止,一切都很好。然后,该图像将显示在年份旁边的子菜单中,这就是我被难住的地方。

我遇到的麻烦是弄清楚我将如何在子菜单中检查该字段并获取它。

我将在下面包含自定义导航步行器的代码。任何帮助将不胜感激!

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";
    }
}
4

1 回答 1

0

万一将来有人遇到这个相当小众的问题,我想出了我需要的方法。

将以下代码块添加到我在原始问题中包含的内容的末尾就可以了。

密切注意我如何检索 ACF 图像字段(获取分类字段的标准方法,如果您不熟悉它),使用$item->object$item->object_id.

然后只是改变输出以包含每个菜单项的背景图像的情况,使用条件检查所述项目是否是类别链接并且是正确的深度(即,如果它是子菜单项)。如果不是这两者,它就不会打扰背景图像。

function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {

    if ( in_array( 'menu-item-object-category', $item->classes ) && $depth > 0 ) {

        // Get the ACF PRO image field (I have replaced my field with 'field_name', change this to your field)
        $thumb = get_field( 'field_name', $item->object . '_' . $item->object_id );

        $before = '<li class="' . implode( ' ', $item->classes ) . '">';
        $after = '</li>';

        // 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 .= ' style="background-image: url(' . $thumb['url']  . ')"';

        $item_output = sprintf( '%1$s<a%2$s>%3$s%4$s%5$s</a>%6$s',
            $before,
            $attributes,
            $args->link_before,
            apply_filters( 'the_title', $item->title, $item->ID ),
            $args->link_after,
            $after
        );

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

    } else {

        $before = '<li class="' . implode( ' ', $item->classes ) . '">';

        // 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 ) .'"' : '';

        $item_output = sprintf( '%1$s<a%2$s>%3$s%4$s%5$s</a>%6$s',
            $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 );

    }

}

function end_el( &$output, $item, $depth = 0, $args = array() ) {
    $output .= "</li>\n";
}
于 2017-06-07T23:05:39.273 回答