繁体   English   中英

将CSS放入Wordpress插件中

[英]Enqueue CSS in Wordpress Plugin

我的机智在这里...这是我用来尝试在CSS插件中使用CSS文件的代码:

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钩子之前,脚本和样式不应注册或排队。 请参阅WordPress中的调试以获取更多信息。 (此消息是在版本3.3中添加的。)在第3547行的/home2/jakereva/public_html/wp-includes/functions.php中

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

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' );

将挂钩名称更改为wp_head。 这将在标题中加入样式。

查看WordPress操作参考以查看正在调用的操作。 https://codex.wordpress.org/Plugin_API/Action_Reference

在前面,您可以使用wp_head。 在管理页面中:admin_enqueue_scripts

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM