當前位置:首頁>WordPress建站>WordPress開發(fā)>WordPress 基于評論者的用戶角色確定評論樣式

WordPress 基于評論者的用戶角色確定評論樣式

本文介紹如何按照Wordpress評論者的角色確定評論的顯示樣式,以及如何讓評論的class更干凈。

定義一個按照給定的用戶ID返回其角色的函數(shù)

/*
Retrieve a commentator's role by the user ID
*/
function cwp_get_user_role( $user_id=null ){
    if ( ! isset( $user_id ) ) return null;
    $user = new WP_User( $user_id );
    $wp_roles = new WP_Roles;
    $role_names    = $wp_roles->get_names();
    $user_role_array      = array ();
    foreach ( $user->roles as $role ){
        if ( isset ( $role_names[ $role ] ) )
            $user_role_array[ $role ] = $role_names[ $role ];
    }
    return key( $user_role_array );
}

為WORDPRESS的COMMENT_CLASS 添加過濾器

/*
Add filter to  comment_class for your WordPress
*/
function cwp_add_comment_author_roles_to_comment_class( $classes, $class, $comment_id, $post_id ){
    //Clean out the unnecessary classes for your comment list item.
    //if you want to use it,uncomment the following sentence.
    /*  $classes=cwp_clean_out_unnecessary_comment_classes($classes);*/
    $comment = get_comment( $comment_id );
    $user_id = (int)$comment->user_id;
    if ( 0 != $user_id ){
        $role = cwp_get_user_role( $user_id );
        if ( !isset( $role ) || empty( $role ) ) return $classes;
        $classes[] = $role;
    }
    return $classes;
}
add_filter( 'comment_class', 'cwp_add_comment_author_roles_to_comment_class', 10, 4 );

現(xiàn)在看看HTML輸出情況:

<li class="comment byuser comment-author-pw even thread-odd thread-alt depth-1 contributor" id="comment-18">
<!--此評論的內容部分-->
</li>

看到了吧,代表角色的”contributor“已經按照預想正確輸出了,但是comment_class默認輸出了很多classes,但是并不是都有用的,下面定義一個清理函數(shù),讓清理過的classes僅保留comment:

/*
Clean out the unnecessary classes for your comment list item.
*/
function cwp_clean_out_unnecessary_comment_classes($classes){
    foreach( $classes as $key => $class ) {
        switch( $class ) {
            case 'comment':
                continue;
            default:
                unset( $classes[$key] );
                continue;
            break;
        }
    }
    unset($key,$class);
    return $classes;
}

添加了上述代碼后,再在’cwp_add_comment_author_roles_to_comment_class‘函數(shù)中按照說明將被注釋掉的那一句啟用,然后再看看輸出了什么:

<li class="comment contributor" id="comment-18">
<!--the comment item content is here-->
</li>

基于用戶角色決定評論樣式

剩下要做的事情就是根據上述輸出的代表角色的class來定義css了,這個,得你自己按照需要來做。

聲明:本站所有文章,如無特殊說明或標注,均為本站原創(chuàng)發(fā)布。任何個人或組織,在未征得本站同意時,禁止復制、盜用、采集、發(fā)布本站內容到任何網站、書籍等各類媒體平臺。如若本站內容侵犯了原著者的合法權益,可聯(lián)系我們進行處理。

給TA打賞
共{{data.count}}人
人已打賞
歡迎關注WordPress大學公眾號 WPDAXUE
WordPress開發(fā)

WordPress 使用 smtp 發(fā)送評論提醒郵件

2014-4-28 8:44:16

WordPress開發(fā)

WordPress 非官方主題/插件添加在線升級功能

2014-4-30 12:45:49

11 條回復 A文章作者 M管理員
  1. 我想問下,如何讓這個css只修改comment-author vcard這個的樣式怎么做呢?我想實現(xiàn)的是評論者名字后面添加一個小圖片,類似于博客的大v那樣的效果;

  2. 這個代碼要如何使用呢?加到主題的functions.php文件里么?加進去之后如何在前臺需要的位置調用出來呢?

  3. 哈哈,這個貌似可以實現(xiàn)站長認證,VIP會員之類的代碼…

  4. coolwp.net

    第二個函數(shù)中的
    if ( ! isset( $role ) || emptyempty( $role ) ) return $classes;
    應該為empty( $role )

  5. 函數(shù)名字能不能再長一點…………

    • 可以更長一點,哈哈。

?
個人中心
購物車
優(yōu)惠劵
今日簽到
有新私信 私信列表
搜索

阿坝| 武定县| 西昌市| 阿拉善右旗| 年辖:市辖区| 邵武市| 图们市| 榆林市| 清水河县| 大洼县| 武平县| 定兴县| 通州区| 连山| 集安市| 三河市| 梁平县| 德阳市| 杭州市| 山西省| 乌鲁木齐县| 澎湖县| 临夏市| 荔浦县| 南丰县| 鸡西市| 乌鲁木齐县| 凤翔县| 海口市| 怀集县| 临安市| 平乐县| 博湖县| 綦江县| 秀山| 扬中市| 斗六市| 三门县| 安泽县| 遂溪县| 昌图县|