2016-05-15 48 views
2

发布此处(而不是wordpress stackexchange)的原因是因为我有一种感觉,这可能与无效PHP有关。似乎更多的用户在这里熟悉php编码。WordPress的自定义沃克菜单不工作

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

enter image description here

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

但是我需要加载自定义walker来呈现菜单。那么这里有什么问题?至于我可以看到我已经正确加载模板和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"; 
    } 
} 

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

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

+0

请编辑您的问题以包含代码。浏览器控制台或PHP错误日志中是否有错误消息?如果该图标无限期旋转,则可能由于PHP错误而导致Ajax请求收到错误消息。 –

+0

您要链接的博客表明代码适用于WordPress 3,因此它可能与WordPress 4.4+(也许)不兼容。 –

回答

1

在我的functions.php我代替:

// Include the custom walker navigation 
require get_template_directory() . '/inc/css_menu_walker.php'; 

/** 
* Custom walker navigation 
*/ 
require get_template_directory() . '/inc/css-menu-walker.php'; 

完整的区别就在这里:https://www.diffnow.com/?report=ba2d5

我也有一些压痕<?php之前我CSS-开始我删除了menu-walker.php。这解决了这个问题。