WordPress子菜单中的ACF PRO分类术语字段

时间:2017-05-25 19:12:32

标签: wordpress advanced-custom-fields megamenu wp-nav-walker

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

我目前正在开发一个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";
    }
}

1 个答案:

答案 0 :(得分:0)

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

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

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

然后只是改变输出以包括每个菜单项的背景图像的情况,使用条件来检查所述项是否是类别链接并且是正确的深度(即,如果它是子菜单项)。如果不是这两个,那就不会对背景图片感到烦恼。

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";
}
相关问题