WordPress"wp_register_style被错误地调用"?

Dan*_*rst 6 php wordpress

我已经谷歌到处寻找答案,但找不到任何体面的答案和实际解决方案.所以,我首先要解释一下我的问题.

我在最新版本的WordPress中使用了我制作的自定义主题.

我想做正确的事情,而不是将我的样式和脚本硬编码到header.php文件中,而是使用WordPress函数将它们排队.

以下是启用调试时显示的注意事项:

Notice: wp_register_style was called incorrectly. Scripts and styles should not be registered or enqueued until the wp_enqueue_scripts, admin_enqueue_scripts, or login_enqueue_scripts hooks. Please see Debugging in WordPress for more information. (This message was added in version 3.3.) in C:\xampp\htdocs\my-project\wp-includes\functions.php on line 3560

Notice: wp_register_style was called incorrectly. Scripts and styles should not be registered or enqueued until the wp_enqueue_scripts, admin_enqueue_scripts, or login_enqueue_scripts hooks. Please see Debugging in WordPress for more information. (This message was added in version 3.3.) in C:\xampp\htdocs\my-project\wp-includes\functions.php on line 3560

Notice: wp_register_style was called incorrectly. Scripts and styles should not be registered or enqueued until the wp_enqueue_scripts, admin_enqueue_scripts, or login_enqueue_scripts hooks. Please see Debugging in WordPress for more information. (This message was added in version 3.3.) in C:\xampp\htdocs\my-project\wp-includes\functions.php on line 3560

Notice: wp_register_script was called incorrectly. Scripts and styles should not be registered or enqueued until the wp_enqueue_scripts, admin_enqueue_scripts, or login_enqueue_scripts hooks. Please see Debugging in WordPress for more information. (This message was added in version 3.3.) in C:\xampp\htdocs\my-project\wp-includes\functions.php on line 3560

Notice: wp_register_script was called incorrectly. Scripts and styles should not be registered or enqueued until the wp_enqueue_scripts, admin_enqueue_scripts, or login_enqueue_scripts hooks. Please see Debugging in WordPress for more information. (This message was added in version 3.3.) in C:\xampp\htdocs\my-project\wp-includes\functions.php on line 3560

Notice: wp_enqueue_style was called incorrectly. Scripts and styles should not be registered or enqueued until the wp_enqueue_scripts, admin_enqueue_scripts, or login_enqueue_scripts hooks. Please see Debugging in WordPress for more information. (This message was added in version 3.3.) in C:\xampp\htdocs\my-project\wp-includes\functions.php on line 3560

Notice: wp_enqueue_style was called incorrectly. Scripts and styles should not be registered or enqueued until the wp_enqueue_scripts, admin_enqueue_scripts, or login_enqueue_scripts hooks. Please see Debugging in WordPress for more information. (This message was added in version 3.3.) in C:\xampp\htdocs\my-project\wp-includes\functions.php on line 3560

Notice: wp_enqueue_style was called incorrectly. Scripts and styles should not be registered or enqueued until the wp_enqueue_scripts, admin_enqueue_scripts, or login_enqueue_scripts hooks. Please see Debugging in WordPress for more information. (This message was added in version 3.3.) in C:\xampp\htdocs\my-project-new\wp-includes\functions.php on line 3560

Notice: wp_enqueue_script was called incorrectly. Scripts and styles should not be registered or enqueued until the wp_enqueue_scripts, admin_enqueue_scripts, or login_enqueue_scripts hooks. Please see Debugging in WordPress for more information. (This message was added in version 3.3.) in C:\xampp\htdocs\my-project\wp-includes\functions.php on line 3560

Notice: wp_enqueue_script was called incorrectly. Scripts and styles should not be registered or enqueued until the wp_enqueue_scripts, admin_enqueue_scripts, or login_enqueue_scripts hooks. Please see Debugging in WordPress for more information. (This message was added in version 3.3.) in C:\xampp\htdocs\my-project\wp-includes\functions.php on line 3560
Run Code Online (Sandbox Code Playgroud)

这是我functions.php文件中的相关部分:

/**
 * Register global styles & scripts.
 */
wp_register_style('my-fonts', '//fonts.googleapis.com/css?family=Lato:300,400,700,900');
wp_register_style('fontawesome', '//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css');
wp_register_style('my-styles', get_template_directory_uri() . '/assets/css/main.css');

wp_register_script('bootstrap', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js', array( 'jquery' ));
wp_register_script('scripts', get_template_directory_uri() . '/assets/js/scripts.js', array( 'jquery' ));


/**
 * Enqueue global styles & scripts.
 */

wp_enqueue_style('my-styles');
wp_enqueue_style('my-fonts');
wp_enqueue_style('fontawesome');

wp_enqueue_script('bootstrap');
wp_enqueue_script('scripts');
Run Code Online (Sandbox Code Playgroud)

我从我需要以某种指定加载的WordPress的"调试通知书猜测wp_enqueue_scripts,admin_enqueue_scriptslogin_enqueue_scripts钩"

Dan*_*rst 20

好吧,我发现了我做错了什么!这是我为任何感兴趣的人做的事情:

我改变了这个:

/**
 * Register global styles & scripts.
 */
wp_register_style('my-fonts', '//fonts.googleapis.com/css?family=Lato:300,400,700,900');
wp_register_style('fontawesome', '//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css');
wp_register_style('my-styles', get_template_directory_uri() . '/assets/css/main.css');

wp_register_script('bootstrap', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js', array( 'jquery' ));
wp_register_script('scripts', get_template_directory_uri() . '/assets/js/scripts.js', array( 'jquery' ));


/**
 * Enqueue global styles & scripts.
 */

wp_enqueue_style('my-styles');
wp_enqueue_style('my-fonts');
wp_enqueue_style('fontawesome');

wp_enqueue_script('bootstrap');
wp_enqueue_script('scripts');
Run Code Online (Sandbox Code Playgroud)

对此:

function my_scripts() {
/**
 * Register global styles & scripts.
 */
wp_register_style('my-fonts', '//fonts.googleapis.com/css?family=Lato:300,400,700,900');
wp_register_style('fontawesome', '//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css');
wp_register_style('my-styles', get_template_directory_uri() . '/assets/css/main.css');

wp_register_script('bootstrap', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js', array( 'jquery' ));
wp_register_script('scripts', get_template_directory_uri() . '/assets/js/scripts.js', array( 'jquery' ));


/**
 * Enqueue global styles & scripts.
 */

wp_enqueue_style('my-styles');
wp_enqueue_style('my-fonts');
wp_enqueue_style('fontawesome');

wp_enqueue_script('bootstrap');
wp_enqueue_script('scripts');
}
Run Code Online (Sandbox Code Playgroud)

并补充说:

add_action( 'wp_enqueue_scripts', 'my_scripts' );
Run Code Online (Sandbox Code Playgroud)

  • 注意,代码样本A和代码样本B之间的区别是在第1行上添加了一个函数“ function y_scripts()”。 (2认同)