WordPress 的自定义 Walker 菜单不起作用

Are*_*ete 5 php wordpress

之所以在这里发布此内容(而不是在 wordpress stackexchange 上)是因为我有一种感觉,这可能与无效的 PHP 有关。看来更多的用户熟悉这里的 php 编码。

我有一个奇怪的问题,我无法通过媒体库将图像添加到帖子中。我可以上传图像,但 WordPress 不会在媒体库中显示之前上传的图像,因此我无法将它们插入到新帖子中。基本上媒体库不获取任何图像。

在此输入图像描述

到目前为止,我发现这个问题是我的主题特有的,并且require get_template_directory() . '/inc/css_menu_walker.php';从我的functions.php 中删除这一行:解决了这个问题。

但是我需要加载自定义步行器来呈现菜单。那么这里出了什么问题呢?据我所知,我已经正确加载了模板,并且在 css_menu_walker.php 中找不到任何错误:

<?php
class CSS_Menu_Maker_Walker extends Walker {

  var $db_fields = array( 'parent' => 'menu_item_parent', 'id' => 'db_id' );

  function start_lvl( &$output, $depth = 0, $args = array() ) {
    $indent = str_repeat("\t", $depth);
    $output .= "\n$indent<ul>\n";
  }

  function end_lvl( &$output, $depth = 0, $args = array() ) {
    $indent = str_repeat("\t", $depth);
    $output .= "$indent</ul>\n";
  }

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

    global $wp_query;
    $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
    $class_names = $value = ''; 
    $classes = empty( $item->classes ) ? array() : (array) $item->classes;

    /* Add active class */
    if(in_array('current-menu-item', $classes)) {
      $classes[] = 'active';
      unset($classes['current-menu-item']);
    }

    /* Check for children */
    $children = get_posts(array('post_type' => 'nav_menu_item', 'nopaging' => true, 'numberposts' => 1, 'meta_key' => '_menu_item_menu_item_parent', 'meta_value' => $item->ID));
    if (!empty($children)) {
      $classes[] = 'has-sub';
    }

    $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
    $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';

    $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
    $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';

    $output .= $indent . '<li' . $id . $value . $class_names .'>';

    $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 = $args->before;
    $item_output .= '<a'. $attributes .'><span>';
    $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
    $item_output .= '</span></a>';
    $item_output .= $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";
  }
}
Run Code Online (Sandbox Code Playgroud)

我自己没有编写自定义步行者。我从这里得到它: http: //cssmenumaker.com/blog/wordpress-3-drop-down-menu-tutorial,似乎这个页面自 2013 年以来就没有更新过。这让我认为代码可能已经过时了并且与当前版本的 WordPress 不兼容。

WordPress 版本并不重要。我在 4.5.2 和 4.4.3 中也遇到同样的问题。

Are*_*ete 3

在我的functions.php中我替换了:

// Include the custom walker navigation
require get_template_directory() .  '/inc/css_menu_walker.php';
Run Code Online (Sandbox Code Playgroud)

/**
 * Custom walker navigation
 */
require get_template_directory() . '/inc/css-menu-walker.php';
Run Code Online (Sandbox Code Playgroud)

此处完全不同:https://www.diffnow.com/?report=ba2d5

<?php我之前在 css-menu-walker.php 的开头也有一些缩进,我删除了它。这解决了问题。