无法获取用户个人资料页面上自定义字段的 WP 用户数据

问题描述 投票:0回答:1

过去 2 天我尝试配置 WP Single 页面上的 Telegram 链接错误,但没有成功。这就是我试图实现的目标:

  1. 我使用functions.php中的php代码在WP的用户个人资料页面上删除并添加了一些字段:

     if ( ! function_exists( 'modify_profile_fields' ) ) :
    
         function modify_profile_fields( $contactmethods ) {
             unset($contactmethods['facebook']);
             unset($contactmethods['youtube']);
             unset($contactmethods['linkedin']);
             unset($contactmethods['instagram']);
             unset($contactmethods['myspace']);
             unset($contactmethods['twitter']);
             unset($contactmethods['pinterest']);
             unset($contactmethods['soundcloud']);
             unset($contactmethods['tumblr']);
             unset($contactmethods['wikipedia']);
    
             $contactmethods['phone1'] = __( 'Phone 1' );
             $contactmethods['telegram1'] = __( 'Telegram username' );
    
             return $contactmethods;
         }
         add_filter('user_contactmethods','modify_profile_fields', 10, 1);
    
     endif;
    

它们按照预期完美正常工作。

  1. 但是,当我尝试在 WordPress 的单页(single.php)上使用以下代码检索最后 2 个字段(phone1 和 telegram1)的数据时,显示 Telegram 字段的值时出现错误:

a.这是 php 部分:

    <?php

    $curauth_id = get_current_user_id();

    $author_phone1 = get_the_author_meta('phone1', $curauth_id);  // Get data from phone1 field on profile page
    $phone_display1 = preg_replace("|\s|","",$author_phone1);     // Remove white space from phone number

    $telegram_input1 = get_the_author_meta('telegram1', $author_id);
    $telegram_username1 = str_replace('@', '', $telegram_input1);   // Remove @ from telegram username if it is entered by users

    ?>

b.这是 HTML 部分:

    {if !empty($author_phone1)}
      <button class="btn phone"><i class="fas fa-phone-alt" style="color:#AF0404 !important;"></i><a href="tel:{$phone_display1}" target="_top"> Call</a></button>
    {/if}

    {if !empty($telegram_username1)}
      <button class="btn chat"><i class="fas fa-comment-dots" style="color:#892cdc !important;"></i><a href="https://t.me/{$telegram_username1}" target="_top"> Chat</a></button>
    {/if}

上面的HTML代码将显示两个按钮:

    Call    Chat

将鼠标悬停或单击“呼叫”按钮时,它可以完美地按预期工作

将鼠标悬停或单击“聊天”按钮时,它会显示或转到:https://t.me/Array - 这意味着上面的 php 代码未检索 Telegram1 字段的值或卷曲代码不正确?

非常感谢任何支持。

php wordpress profile
1个回答
0
投票

WOW:我发现WP个人资料页面不允许使用Telegram、telegram1等词作为个人资料表单自定义字段的css id和名称;这可能是 WP 本身或我的 WP 主题为单页设计的一个技巧。当我将 telegram1 更改为 email2 时,它按预期完美运行。有人信吗?

这是我完整的工作代码:

  1. 我使用functions.php中的php代码在WP的用户个人资料页面上删除并添加了一些字段:

     if ( ! function_exists( 'modify_profile_fields' ) ) :
    
         function modify_profile_fields( $contactmethods ) {
             unset($contactmethods['facebook']);
             unset($contactmethods['youtube']);
             unset($contactmethods['linkedin']);
             unset($contactmethods['instagram']);
             unset($contactmethods['myspace']);
             unset($contactmethods['twitter']);
             unset($contactmethods['pinterest']);
             unset($contactmethods['soundcloud']);
             unset($contactmethods['tumblr']);
             unset($contactmethods['wikipedia']);
    
             $contactmethods['phone1'] = __( 'Phone 1' );
             $contactmethods['email2'] = __( 'Telegram username' );
    
             return $contactmethods;
         }
         add_filter('user_contactmethods','modify_profile_fields', 10, 1);
    
     endif;
    

它们按照预期完美正常工作。

  1. 但是,当我尝试在 WordPress 的单页(single.php)上使用以下代码检索最后 2 个字段(phone1 和 telegram1)的数据时,显示 Telegram 字段的值时出现错误:

a.这是 php 部分:

    <?php

    $curauth_id = get_current_user_id();

    $author_phone1 = get_the_author_meta('phone1', $curauth_id);  // Get data from phone1 field on profile page
    $phone_display1 = preg_replace("|\s|","",$author_phone1);     // Remove white space from phone number

    $telegram_input1 = get_the_author_meta('email2', $author_id);
    $telegram_username1 = str_replace('@', '', $telegram_input1);   // Remove @ from telegram username if it is entered by users

    ?>

b.这是 HTML 部分:

    {if !empty($author_phone1)}
      <button class="btn phone"><i class="fas fa-phone-alt" style="color:#AF0404 !important;"></i><a href="tel:{$phone_display1}" target="_top"> Call</a></button>
    {/if}

    {if !empty($telegram_username1)}
      <button class="btn chat"><i class="fas fa-comment-dots" style="color:#892cdc !important;"></i><a href="https://t.me/{$telegram_username1}" target="_top"> Chat</a></button>
    {/if}
© www.soinside.com 2019 - 2024. All rights reserved.