2015-05-30 35 views
-1

我想为我的wordpress菜单设置一个自定义步行者。其目的是为了让我的PHP代码添加到菜单链接,我可以用文字和图片是这样的:“数组必须有两个成员”是什么意思?

<a href="http://mcadrives.com/?ref=<?PHP code goes here?>"> 

我一直在使用的每一个超链接和图像的PHP代码是这样的:

<a href="http://mcadrives.com/?ref= 
    <?php 
     if (!empty ($_SERVER['QUERY_STRING'])){ 
     echo substr($_SERVER['QUERY_STRING'],4); } 
     else{ 
     echo 'mhammonds'; } 
    ?>> 

我将此添加到我的functions.php文件:

class Query_Nav extends Walker_Nav_Menu 
{ 
    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; 
     $classes[] = 'menu-item-' . $item->ID; 

     $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  ) .'"' : ''; 

     //ADD YOUR PHP HERE TO DETERMINE WHATEVER IT IS YOU NEED FOR YOUR LINK 
     if (! empty ($_SERVER['QUERY_STRING'])) { 
      $addedStuff = '?ref=' . substr($_SERVER['QUERY_STRING'], 4); 
      }else { 
        $addedStuff = '?ref=mhammonds'; 
} 
     $attributes .= ! empty($item->url)  ? ' href="' . esc_attr($item->url.$addedStuff) .'"' : ''; 
     //////////////////// 

     $item_output = $args->before; 
     $item_output .= '<a'. $attributes .'>'; 
     $item_output .= $args->link_before . apply_filters('the_title', $item->title, $item->ID) . $args->link_after; 
     $item_output .= '</a>'; 
     $item_output .= $args->after; 

     $output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args); 
    } 
} 

和我说这个学步车 '新Query_Nav' 我的导航菜单-的template.php:

function wp_nav_menu($args = array()) { 
    static $menu_id_slugs = array(); 

    $defaults = array('menu' => '', 'container' => 'div', 'container_class' => '', 'container_id' => '', 'menu_class' => 'menu', 'menu_id' => '', 
    'echo' => true, 'fallback_cb' => 'wp_page_menu', 'before' => '', 'after' => '', 'link_before' => '', 'link_after' => '', 'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>', 
    'depth' => 0, 'walker' => 'new Query_Nav()', 'theme_location' => ''); 

我当时被警告类的新Query_Nav()“在660行中没有找到通知,所以我说像这样:

function walk_nav_menu_tree($items, $depth, $r) { 
    $walker = (empty($r->walker)) ? new Walker_Nav_Menu : $r->walker; 
    $args = array($items, $depth, $r); 

    return call_user_func_array(array($walker, 'walk', 'new Query_Nav()'), $args); 

现在我还有一个警告,指出:

call_user_func_array() expects parameter 1 to be a valid callback, array must have exactly two members in /home/ballaboy258/public_html/wp-includes/nav-menu-template.php on line 660 

我已经删除了每一个,都没有解决问题。如果我删除$ walker,则找不到“walk”。如果我删除'走',那么'新的Query_Nav()'找不到。如果我删除'new Query_Nav()',再次找不到'新的Query_Nav()',这没有任何意义。 我错过了一个步骤,还是我的语法错了?我很困惑,我可以“T在网络上找到任何事情来帮助我。

回答

0

这意味着,你的回调阵列

array($walker, 'walk', 'new Query_Nav()'), 

应该包括两个因素

  • 的方法

表示您呼叫

你提供了三种

+0

我已删除每一个既不已经解决了这个问题的功能。如果我删除$ walker,则找不到“walk”。如果我删除'走',那么'新的Query_Nav()'找不到。如果我删除'new Query_Nav()',再次找不到'新的Query_Nav()',这没有任何意义。@MarkBaker – Marquell

相关问题