【问题标题】:ACF PRO taxonomy term field in WordPress submenuWordPress 子菜单中的 ACF PRO 分类术语字段
【发布时间】:2017-06-08 07:05:39
【问题描述】:

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

我目前正在开发一个需要自定义子菜单的 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";
    }
}

【问题讨论】:

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


    【解决方案1】:

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

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

    密切注意我如何使用$item-&gt;object$item-&gt;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";
    }
    

    【讨论】:

      猜你喜欢
      • 2020-06-21
      • 2015-02-13
      • 2018-07-20
      • 2018-08-21
      • 2017-09-20
      • 2019-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多