警告:wordpress网站上的array_key_exists(),我该如何解决?

时间:2013-12-04 00:44:21

标签: php wordpress

我在wordpress网站上看到以下错误(奇怪的是它只在用户登录时出现)..有什么想法吗?谢谢!

  Warning: array_key_exists() [function.array-key-exists]: The first argument should be 
either a string or an integer in wp-content/plugins/display-name-author-permalink/display-name-author-permalink.php on line 55

以下是代码:

    class DisplayNameAuthorPermaLink {

    var $users = array();

    // Build an array of usernames and display names and increment duplicates for uniqueness
    function __construct() {
        $i = 1;
        foreach ( get_users() as $user ) { 
            $display_name = $display_name = sanitize_title($user->display_name);
            if ( in_array(sanitize_title($user->display_name), $this->users) ) {
                $i++;
                $display_name .= "-$i"; 
            }
                $this->users[sanitize_title($user->user_login)] = $display_name;
        }
        add_action('pre_get_posts', array(&$this, 'switch_author'));
        add_filter('author_link', array(&$this, 'filter_author'), 10, 3);
    }


    // Switch the display name with the username so that we can populate the posts properly
    // If the username was used in the call do a 404 template redirection
    function switch_author() {
        if ( ! is_author() ) 
            return;
        $author_name = get_query_var('author_name');
        $key = array_search($author_name, $this->users);
            if ( $key ) {
                    set_query_var('author_name', $key);
                    $author = get_user_by('login', $key);
                    set_query_var('author', $author->ID);
            } else {
            set_query_var('author_name', false);
            set_query_var('author', false);
            add_action('template_redirect', array(&$this, 'redirect_404'));
        }
    }

    // Replace the username in author links generated in the theme with the users display name
    function filter_author($link,$author_id,$author_nicename) {
            if ( array_key_exists($author_nicename, $this->users) )
                    $link = str_replace($author_nicename,$this->users[$author_nicename], $link);
            return $link;
    }

    // redirect template to use 404 template
    function redirect_404() {
        include(get_404_template());
        die();
    }

}

// Instantiate the DisplayNameAuthorPermaLink class
$DisplayNameAuthorPermaLink = new DisplayNameAuthorPermaLink();

0 个答案:

没有答案