我已经到处搜索答案,但找不到任何具有实际解决方案的合适答案。所以,我先解释一下我的问题。
我正在使用我在最新版本的 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
这是我的 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');
我从调试通知中猜测我需要以某种方式指定加载 WordPress 的“wp_enqueue_scripts
、admin_enqueue_scripts
或 login_enqueue_scripts
Hook ”
最佳答案
好吧,我发现我做错了什么!这是我为任何感兴趣的人所做的:
我改变了这个:
/**
* 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');
对此:
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');
}
并添加了这个:
add_action( 'wp_enqueue_scripts', 'my_scripts' );
关于php - WordPress "wp_register_style was called incorrectly"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31649063/