亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將評論作者姓名更改為僅在 WooCommerce 中帶有星號的第一個和最后一個字母

將評論作者姓名更改為僅在 WooCommerce 中帶有星號的第一個和最后一個字母

PHP
烙印99 2023-06-18 16:35:35
我正在使用“如何在 WooCommerce 中更改評論作者顯示名稱”,以便我可以將網站上顯示的評論作者更改為名字后跟姓氏首字母。add_filter('get_comment_author', 'my_comment_author', 10, 1);function my_comment_author( $author = '' ) {? ? // Get the comment ID from WP_Query? ? $comment = get_comment( $comment_ID );? ? if (!empty($comment->comment_author) ) {? ? ? ? if($comment->user_id > 0){? ? ? ? ? ? $user=get_userdata($comment->user_id);? ? ? ? ? ? $author=$user->first_name.' '.substr($user->last_name,0,1).'.'; // this is the actual line you want to change? ? ? ? } else {? ? ? ? ? ? $author = __('Anonymous');? ? ? ? }? ? } else {? ? ? ? $author = $comment->comment_author;? ? }? ? return $author;}我需要做的是只使用名字(不顯示姓氏)并將除第一個和最后一個字符之外的所有字符更改為“*”。例如 James 變成 J***s 而 Michael 變成 M*****l
查看完整描述

1 回答

?
aluckdog

TA貢獻1847條經驗 獲得超7個贊

調整是:

  • 顯示名字

  • 詹姆斯成為J***s

在代碼中添加了注釋和解釋

function my_comment_author( $author, $comment_id, $comment ) {  

    // NOT empty

    if ( $comment ) {

        // Get user id

        $user_id = $comment->user_id;


        // User id exists

        if( $user_id > 0 ) {

            // Get user data

            $user = get_userdata( $user_id );


            // User first name

            $user_first_name = $user->first_name;


            // Call function

            $author = replace_with_stars( $user_first_name );       

        } else {

            $author = __('Anonymous', 'woocommerce');

        }

    }


    return $author;

}

add_filter('get_comment_author', 'my_comment_author', 10, 3 );


function replace_with_stars( $str ) {

    // Returns the length of the given string.

    $len = strlen( $str );


    return substr( $str, 0, 1 ).str_repeat('*', $len - 2).substr( $str, $len - 1, 1 );

}


查看完整回答
反對 回復 2023-06-18
  • 1 回答
  • 0 關注
  • 208 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號