如何在 WordPress 中顯示 Twitter 關注者計數為文本

已發表: 2023-06-15

您想在 WordPress 中將您的 Twitter 關注者計數顯示為文本嗎?

通過顯示很多人在社交媒體上關注您,您可以鼓勵訪問者信任您的網站。 更好的是,通過將此信息顯示為文本,您可以自由地在您網站的任何地方使用它,包括在您的帖子和頁面內。

在本文中,我們將展示如何在 WordPress 中將您的 Twitter 關注者計數顯示為文本。

How to display Twitter followers count as text in WordPress

為什麼在 WordPress 中顯示 Twitter 關注者算作文本?

您可能已經註意到,許多流行的博客、有影響力的人和品牌都自豪地顯示有多少人在社交媒體上關注他們。

如果訪問者看到很多人在社交媒體上關注您,那麼他們就更有可能信任您的企業,並將您視為博客領域的專家。

許多最好的社交媒體插件允許您在嵌入式提要、按鈕、橫幅等中顯示關注者總數。

但是,有時您可能希望將數字顯示為純文本。 這使您可以自由地將關注者數量添加到您的博客文章、頁腳或您的 WordPress 博客或網站上的任何其他位置。

考慮到這一點,讓我們看看如何在 WordPress 中將 Twitter 關注者計數顯示為文本。

第 1 步:獲取 Twitter API 密鑰和秘密

要獲得您的關注者數量,您需要通過創建 API 密鑰和密鑰來訪問 Twitter API。

要獲取此信息,請前往 Twitter 開發者門戶,然後單擊“註冊免費帳戶”。

Signing up for a Twitter Developers account

您現在可以輸入一些關於您計劃如何使用 Twitter API 的信息。 最好提供盡可能詳細的信息,因為 Twitter 會審查這些信息,如果他們不了解您如何使用他們的 API,可能會刪除您的帳戶。

之後,閱讀條款和條件。 如果您願意繼續,請繼續並單擊“提交”按鈕。

Agreeing to the Twitter Developers terms

您現在將看到 Developer Portal。 在左側菜單中,單擊以展開“項目和應用程序”部分。 然後,選擇“概覽”。

您現在可以繼續並單擊“添加應用程序”。

How to create a Twitter app

之後,只需輸入你想為你的 Twitter 應用程序使用的名稱。 這僅供您參考,因此您可以使用任何您想要的東西。

完成後,單擊“下一步”按鈕。

Naming a Twitter application

Twitter 現在將顯示 API 密鑰和 API 秘密。 這是您唯一一次看到此信息,因此請將其記錄在安全的地方。

我們建議將密鑰和機密添加到密碼管理器以提高安全性。

Getting a Twitter API key and secret

第 2 步:將自定義代碼添加到您的 WordPress 網站

將 Twitter 關注者數量添加到您的站點的最簡單方法是使用 PHP 代碼。

出於安全原因,WordPress 不允許您將 PHP 代碼直接添加到您的頁面和帖子中,但它允許短代碼。 這意味著您可以創建自定義短代碼,然後將其鏈接到您的 PHP 代碼。

在 WordPress 中添加自定義短代碼的最簡單方法是使用 WPCode。 該插件允許您創建任意數量的短代碼,然後將它們鏈接到 PHP 代碼的不同部分。

您需要做的第一件事是安裝並激活免費的 WPCode 插件。 有關更多詳細信息,請參閱我們關於如何安裝 WordPress 插件的分步指南。

激活後,前往代碼片段»添加片段

Adding custom shortcode to your WordPress website

在這裡,您將看到所有可以添加到網站的現成片段。 其中包括允許您完全禁用 WordPress 評論、上傳 WordPress 默認不支持的文件等的片段。

由於您正在創建新代碼段,請將鼠標懸停在“添加您的自定義代碼”上。 然後,只需點擊“使用代碼段”。

Adding a custom code snippet to WordPress using WPCode

首先,輸入自定義代碼片段的標題。 這可以是幫助您識別 WordPress 儀表板中的片段的任何內容。

之後,您需要打開“代碼類型”下拉菜單並選擇“PHP 代碼段”。

Adding a PHP snippet to WordPress using custom code

在代碼編輯器中,只需粘貼以下 PHP 代碼:

function getTwitterFollowers($screenName = 'wpbeginner')
{
    // some variables
    $consumerKey = 'YOUR_CONSUMER_KEY';
    $consumerSecret = 'YOUR_CONSUMER_SECRET';
    $token = get_option('cfTwitterToken');
  
    // get follower count from cache
    $numberOfFollowers = get_transient('cfTwitterFollowers');
  
    // cache version does not exist or expired
    if (false === $numberOfFollowers) {
        // getting new auth bearer only if we don't have one
        if(!$token) {
            // preparing credentials
            $credentials = $consumerKey . ':' . $consumerSecret;
            $toSend = base64_encode($credentials);
  
            // http post arguments
            $args = array(
                'method' => 'POST',
                'httpversion' => '1.1',
                'blocking' => true,
                'headers' => array(
                    'Authorization' => 'Basic ' . $toSend,
                    'Content-Type' => 'application/x-www-form-urlencoded;charset=UTF-8'
                ),
                'body' => array( 'grant_type' => 'client_credentials' )
            );
  
            add_filter('https_ssl_verify', '__return_false');
            $response = wp_remote_post('https://api.twitter.com/oauth2/token', $args);
  
            $keys = json_decode(wp_remote_retrieve_body($response));
  
            if($keys) {
                // saving token to wp_options table
                update_option('cfTwitterToken', $keys->access_token);
                $token = $keys->access_token;
            }
        }
        // we have bearer token wether we obtained it from API or from options
        $args = array(
            'httpversion' => '1.1',
            'blocking' => true,
            'headers' => array(
                'Authorization' => "Bearer $token"
            )
        );
  
        add_filter('https_ssl_verify', '__return_false');
        $api_url = "https://api.twitter.com/1.1/users/show.json?screen_name=$screenName";
        $response = wp_remote_get($api_url, $args);
  
        if (!is_wp_error($response)) {
            $followers = json_decode(wp_remote_retrieve_body($response));
            $numberOfFollowers = $followers->followers_count;
        } else {
            // get old value and break
            $numberOfFollowers = get_option('cfNumberOfFollowers');
            // uncomment below to debug
            //die($response->get_error_message());
        }
  
        // cache for an hour
        set_transient('cfTwitterFollowers', $numberOfFollowers, 1*60*60);
        update_option('cfNumberOfFollowers', $numberOfFollowers);
    }
  
    return $numberOfFollowers;
}

echo getTwitterFollowers(); ?>

在上面的代碼中,請確保將以下佔位符替換為您自己的 API 密鑰和 API 機密:

    $consumerKey = 'YOUR_CONSUMER_KEY';
    $consumerSecret = 'YOUR_CONSUMER_SECRET';

您還需要將“wpbeginner”替換為您要使用的 Twitter 帳戶。 這可以是任何 Twitter 帳戶,包括您不擁有的帳戶:

function getTwitterFollowers($screenName = 'wpbeginner')

要獲取 Twitter 用戶名,只需在新選項卡中打開 Twitter 個人資料即可。 您將在 URL 和個人資料標題中找到用戶名:

Getting a Twitter username

完成後,切換回 WordPress 儀表板。 在這裡,只需單擊“非活動”開關,使其變為“活動”。

然後您可以繼續並單擊“保存代碼段”按鈕。

Displaying the Twitter follower count using WPCode

完成後,滾動到“插入”部分。

WPCode 可以自動將您的代碼添加到不同的位置,例如在每個帖子之後、僅前端或僅管理員。 要獲取簡碼,只需單擊“簡碼”按鈕。

Adding a Twitter follower count to WordPress using a custom shortcode

您現在可以使用短代碼將社交證明添加到任何頁面或帖子。

在塊編輯器中,只需單擊“+”按鈕並輸入“簡碼”。 當它出現時,選擇簡碼塊將其添加到頁面或帖子。

How to add a shortcode block to WordPress

您現在可以將短代碼添加到塊中。

請注意,簡碼僅顯示關注者總數,因此您通常需要添加一些文本來解釋數字的含義。

Adding a Twitter follower count to WordPress using a custom shortcode

有關如何放置簡碼的更多信息,請參閱我們關於如何在 WordPress 中添加簡碼的指南。

當您對頁面的設置感到滿意時,您可以通過單擊“更新”或“發布”按鈕來實時統計關注者。

現在,如果您訪問您的 WordPress 網站,您將看到實時關注者計數。

An example of a Twitter follower count, created using WPCode

我們希望本教程能幫助您了解如何在 WordPress 中將您的 Twitter 關注者計數顯示為文本。 您可能還想了解如何在 WordPress 中創建自定義 Instagram 照片提要,或者查看我們的專家精選,了解適用於 WordPress 的最佳 Twitter 插件。

如果您喜歡這篇文章,請訂閱我們的 YouTube 頻道以獲取 WordPress 視頻教程。 您還可以在 Twitter 和 Facebook 上找到我們。