php - 在 WordPress 插件中对 CSS 进行排队

标签 php css wordpress stylesheet

我已经无计可施了...这是我用来尝试让 CSS 文件在我的 WordPress 插件中工作的代码:

add_action('wp_enqueue_scripts', 'initial_admin_links_hide_stylesheet');

function initial_admin_links_hide_stylesheet() {
    wp_enqueue_style( 'prefix-style', plugins_url('initial_hide_admin_links.css', __FILE__));   
}

然后我使用这行代码调用该函数:

initial_admin_links_hide_stylesheet();

如果我注释掉对该函数的调用,我不会收到任何通知。如果我不注释它,我收到的通知是:

注意:wp_enqueue_style 被错误调用。在 wp_enqueue_scripts、admin_enqueue_scripts 或 login_enqueue_scripts Hook 之前,不应注册或排队脚本和样式。请参阅 WordPress 中的调试以获取更多信息。 (此消息是在 3.3 版本中添加的。)位于/home2/jakereva/public_html/wp-includes/functions.php 第 3547 行

据我所知,代码已正确编写,但当我调用该函数时,我绝对无法让该通知消失。帮助!提前致谢。

最佳答案

add_action('wp_enqueue_scripts', 'initial_admin_links_hide_stylesheet');

这会告诉 WordPress 将脚本挂接到您的进程中。无需手动调用该函数。

确保主题文件中适当的地方有 wp_head() 和 wp_footer() 调用,或者需要添加它

/**
 * Proper way to enqueue scripts and styles
 */
function theme_name_scripts() {
    wp_enqueue_style( 'style-name', get_stylesheet_uri() );
    wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/example.js', array(), '1.0.0', true );
}

add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );

关于php - 在 WordPress 插件中对 CSS 进行排队,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29498954/

相关文章:

php - 将 PHP 变量值传递到 JQuery 页面

php - 学说 2 文档示例中的拥有方和反方是什么

php - 带有哈希标签的 Paypal 返回网址?

html - 为什么我的 HTML/CSS 网站顶部有空白区域?

css - 如何更改插件的 CSS

css - h1 :after badge not lining up properly

javascript - 在 Wordpress 页面中包含使用 Angular 构建的 html 页面

html - 在汉堡图标旁边添加 "MENU"字 - 对我来说是奇怪的代码

css - 如何将 map 的高度固定为 Angular 4 中的屏幕尺寸

html - 使标题菜单固定位置(主题tesseract)