首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WordPress子菜单中的ACF PRO分类术语字段

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

Stack Overflow用户
提问于 2017-05-26 03:12:32
回答 1查看 451关注 0票数 0

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

我目前正在开发一个WordPress网站,其中自定义子菜单是必需的。在所述子菜单中,显示类别术语的列表(顺便说一下,是年份-2003-2017的列表)。每一年,我都会使用卓越的高级自定义字段PRO插件创建一个图像字段。这个想法是,一张图片可以在“编辑类别”页面上传一年。到现在为止还好。然后,这个图像将显示在年份旁边的子菜单中,这就是我被难住的地方。

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

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

代码语言:javascript
复制
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";
    }
}
EN

回答 1

Stack Overflow用户

发布于 2017-06-08 07:05:39

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

将下面的代码块添加到我在原始问题中包含的内容的末尾,做到了这一点。

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

然后,只需更改输出以包括每个菜单项的背景图像,使用条件检查所述项是否为类别链接以及是否为正确的深度(即,是否为子菜单项)。如果不是这两个,它就不会影响背景图像。

代码语言:javascript
复制
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";
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44188152

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档