警告:call_user_func_array() 期望参数 1 是有效的回调,位于第 287 行的 wp-includ



当我在 functions 中添加 main 函数时,我正在创建我的第一个供个人使用的 WordPress 主题.php,网站顶部显示警告。

警告:call_user_func_array((期望参数 1 是有效的回调,找不到函数 'myfirsttheme_setup' 或 G:\wamp64\www\mysite\wp-include\class-wp-hook.php 第287行中的函数名称无效。

我应该先定义myfirsttheme_setup函数吗?如果是,那怎么做?

我的代码在这里:

<?php
/**
* @package myfirsttheme
* @since myfirsttheme
*/
if ( ! function_exists( 'myfirsttheme_setup' ) ) :
/**
* support post thumbnails.
*/
function myfirsttheme_setup() {
add_theme_support( 'custom-header' );
add_theme_support('menus');
add_theme_support('post-thumbnails');
add_theme_support( 'automatic-feed-links' );
add_theme_support( 'title-tag' );
add_theme_support( 'customize-selective-refresh-widgets' );
}
endif; // myfirsttheme_setup
add_action( 'after_setup_theme', 'myfirsttheme_setup' );

您输入的代码是正确的。myfirsttheme_setup只需要在执行after_setup_theme操作挂钩之前定义。添加挂钩时不需要定义它。动作钩子只会将字符串存储在 $wp_filter 全局中,然后在执行钩子时调用call_user_func_array。您的问题一定在其他地方。我会在您的整个安装过程中搜索myfirsttheme_setup。在定义它或更改myfirsttheme_setup之前,您必须尝试挂接它,以便在另一个地方不可调用。

您始终可以选择将其设置为匿名函数。

add_action('after_setup_theme', function () {
add_theme_support('custom-header');
add_theme_support('menus');
add_theme_support('post-thumbnails');
add_theme_support('automatic-feed-links');
add_theme_support('title-tag');
add_theme_support('customize-selective-refresh-widgets');
});

相关内容

最新更新