什么“数组必须恰好有两个成员”?的意思

What does "array must have exactly two members" mean?

本文关键字:两个 成员 意思 数组 什么      更新时间:2023-09-26

我正在尝试为我的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 );
    }
}

,我添加了这个walker 'new Query_Nav'到我的nav_menu -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' => '' );

然后我被一个警告通知,类'new 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'就找不到。如果我删除'walk',则找不到'new Query_Nav()'。如果我删除'new Query_Nav()',再次,'new Query_Nav()'没有找到。这没有任何意义。是我漏了一步,还是我的语法错了?我很困惑,我在网上找不到任何东西来帮助我。

这意味着你的回调数组

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

应该包含两个元素

表示要调用的函数

您已经提供了三个