如何仅为非登录用户取消注册 Wordpress 样式表



我在functions.php中使用以下代码从前端禁用dashicons.min.css文件。它工作正常,但是我如何仅对当前未登录WordPress的访问者使用该操作?

原因是顶部的WordPress管理栏坏了,如果dashicons css文件不可用。

add_action( 'wp_print_styles',     'my_deregister_styles', 100 );
function my_deregister_styles()    { 
   wp_deregister_style( 'dashicons' ); 
}

你想使用WordPress附带的函数is_user_logged_in()。在 https://developer.wordpress.org/reference/functions/is_user_logged_in/阅读有关它的信息。

所以你的代码将是:

add_action( 'wp_print_styles',        'my_deregister_styles', 100 );
function my_deregister_styles()    {
    if( !is_user_logged_in() ) 
        wp_deregister_style( 'dashicons'); 
}

最新更新