【问题标题】:Load CSS for User role subscriber (Wordpress)为用户角色订阅者加载 CSS (Wordpress)
【发布时间】:2017-05-14 21:55:42
【问题描述】:

我正在尝试加载样式表是用户角色是订阅者,我做错了什么:

<?php
global $current_user; 
get_currentuserinfo(); 
if ( user_can( $current_user, "subscriber" ) ){ 
echo '<link rel="stylesheet" href="/login/subscriber-style.css">'
} 
?>

这是在主题的header.php中

【问题讨论】:

  • 你的代码很好,但是你的css文件在主题导演下,那么你可以像这样使用 wp_enqueue_style ('theme-style', get_template_directory_uri().'/login/subscriber-style.css');
  • 我尝试了绝对链接,但仍然出现 PHP 错误?
  • 请分享您的 php 错误

标签: wordpress


【解决方案1】:

您正在使用一种过时的方法来获取当前用户角色。 get_currentuserinfo() 在 WordPress 4.5 中已弃用。

https://codex.wordpress.org/Function_Reference/get_currentuserinfo

另外,您不应该直接从 header.php 文件加载 CSS,而是需要将其加入队列。

将以下代码添加到您的functions.php 文件中。

function wpse_load_subscriber_stylesheet() {
    if ( current_user_can( 'subscriber' ) ) {
        wp_enqueue_style( 'login-subscriber-style', home_url( '/login/subscriber-style.css' ) );
    } 
}
add_action( 'wp_enqueue_scripts', 'wpse_load_subscriber_stylesheet' );

这假定您的 CSS 位于您的 Web 根目录 (https://example.com/login/subscriber-style.css)。如果它在您的主题文件夹中,那么您需要get_template_directory_uri() . '/login/subscriber-style.css'

https://developer.wordpress.org/reference/functions/wp_enqueue_style/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-19
    • 2013-02-26
    • 2016-12-03
    • 2019-05-06
    • 2020-11-04
    • 2021-11-08
    相关资源
    最近更新 更多