twitter最多关注者_如何在WordPress中将Twitter关注者显示为文本

twitter最多关注者

The easiest way to display Twitter followers is by using the official Twitter follow button. But what if you don’t want to slow your site down by loading twitter’s script? Or what if you are making something very custom and need to display twitter follower count as text instead of a button. Well then you will like this tutorial. In this article, we will show you how to display your twitter follower count as text on your WordPress site.

显示Twitter关注者的最简单方法是使用Twitter官方关注按钮 。 但是,如果您不想通过加载Twitter脚本来减慢网站速度呢? 或者,如果您要进行非常自定义的操作,并且需要将Twitter关注者计数显示为文本而不是按钮,该怎么办。 那么,您会喜欢本教程的。 在本文中,我们将向您展示如何在WordPress网站上以文本形式显示您的Twitter关注者计数。

Wondering how we are going to do this? Well, first we will create a Twitter App, so we can properly use the Twitter API v1.1 to pull the followers count. We will cache it to optimize performance, and then we will display it on the site. Ready to get started? Let’s go.

想知道我们将如何做到这一点? 好吧,首先我们将创建一个Twitter App,以便我们可以正确使用Twitter API v1.1来吸引关注者。 我们将对其进行缓存以优化性能,然后将其显示在站点上。 准备开始了吗? 我们走吧。

First thing you need to do is to create a Twitter App for the site where you want to display the followers count. Go to Twitter Developers website and sign in with your Twitter account. After signing in create a new application.

您需要做的第一件事是为要显示关注者人数的站点创建一个Twitter App。 转到Twitter开发者网站并使用您的Twitter帐户登录。 登录后,创建一个新的应用程序。

Creating a new Twitter app

On the next screen provide a name for your app this could be anything, ideally the title of your website. Provide a description for your app, this could be the same description as your blog or anything you want. In the website field enter the URL of your WordPress site, For example: https://www.wpbeginner.com.

在下一个屏幕上,为您的应用提供一个名称,可以是任何名称,最好是您网站的标题。 为您的应用提供描述,该描述可以与您的博客或任何您想要的描述相同。 在网站字段中,输入您的WordPress网站的URL,例如:https://www.wpbeginner.com。

Enter the same URL in the Callback URL field as well. After filling the form hit the Create your Twitter application button at the bottom of the page.

在“回调URL”字段中也输入相同的URL。 填写表格后,点击页面底部的“ 创建您的Twitter应用程序”按钮。

This will create a new Twitter app for you to use. On the next page, click on Create my access token button. This will show you a notification that your authorization token has been created.

这将创建一个新的Twitter应用供您使用。 在下一页上,单击“ 创建我的访问令牌”按钮。 这将向您显示已创建授权令牌的通知。

On your Twitter App’s page, we will only need the Consumer Key and Consumer Secret for the next step.

在您的Twitter App页面上,下一步只需要使用Consumer Key和Consumer Secret。

Copy the following code and paste it in your theme’s functions.php file or a site specific plugin. Replace Consumer Key and Consumer Secret variables with your consumer key and secret.

复制以下代码,并将其粘贴到主题的functions.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;
}

Now add this line of code in your theme template where you want to display your twitter followers count. This could be in the sidebar.php, header.php, or basically anywhere you like.

现在,将这行代码添加到主题模板中,以显示Twitter粉丝数。 它可以在sidebar.php,header.php中,或者基本上在您喜欢的任何位置。


<?php 
echo getTwitterFollowers('your_screen_name');
 ?>

That’s it. You are done. We hope that this article helped you show Twitter followers as text in WordPress. There are many other things that you can do to integrate twitter with your WordPress site. For example, you can add twitter cards, or display recent tweets in WordPress. To get more such useful tips consider following @wpbeginner on Twitter.

而已。 大功告成 我们希望本文能帮助您以WordPress中的文本形式显示Twitter关注者。 您还可以做很多其他事情来将Twitter与WordPress网站集成在一起。 例如,您可以添加推特卡 ,或在WordPress中显示最近的推文 。 要获取更多此类有用的提示,请考虑在Twitter上关注@wpbeginner

Source: Zvonko Biskup

资料来源: Zvonko Biskup

翻译自: https://www.wpbeginner.com/wp-tutorials/displaying-the-total-number-of-twitter-followers-as-text-on-wordpress/

twitter最多关注者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值