联系我们
简单又实用的WordPress网站制作教学
当前位置:网站首页 > WordPress教程 > 正文

如何在wordpress中显示推特关注者数量和更多

作者:98wpeu发布时间:2024-01-26分类:WordPress教程浏览:104


导读:我们之前写过一个代码,可以让你显示推特关注者的数量,这是由Rarst贡献的在这篇文章中,我们将分享一个更高级、更优雅的代码,它可以让你在wordpress中显示推特追随者的数量。这...

我们之前写过一个代码,可以让你显示推特关注者的数量,这是由Rarst贡献的在这篇文章中,我们将分享一个更高级、更优雅的代码,它可以让你在wordpress中显示推特追随者的数量。这个剧本也是由拉斯特贡献的.

功能

此功能不限于追随者计数.它可以获取用户/显示接口方法返回的任何非嵌套值。

它有两个级别的缓存:

查询值以数组形式存储在数据库中,使用WP选项,存储时间为$Interval秒; 响应存储在内存中,因此您可以查询任意数量的字段,而不会生成多个接口请求接口。

这应该是安全的,可以同时用于乘法值和乘法用户,而不必担心耗尽接口限制。

补习

首先打开您的主题的Functions.php文件并添加以下代码:

    function rarst_twitter_user( $username, $field, $display = false ) {
    $interval = 3600;
    $cache = get_option('rarst_twitter_user');
    $url = 'http://api.twitter.com/1/users/show.json?screen_name='.urlencode($username);

    if ( false == $cache )
    $cache = array();

    // if first time request add placeholder and force update
    if ( !isset( $cache[$username][$field] ) ) {
    $cache[$username][$field] = NULL;
    $cache[$username]['lastcheck'] = 0;
    }

    // if outdated
    if( $cache[$username]['lastcheck'] < (time()-$interval) ) {

    // holds decoded JSON data in memory
    static $memorycache;

    if ( isset($memorycache[$username]) ) {
    $data = $memorycache[$username];
    }
    else {
    $result = wp_remote_retrieve_body(wp_remote_request($url));
    $data = json_decode( $result );
    if ( is_object($data) )
    $memorycache[$username] = $data;
    }

    if ( is_object($data) ) {
    // update all fields, known to be requested
    foreach ($cache[$username] as $key => $value)
    if( isset($data->$key) )
    $cache[$username][$key] = $data->$key;

    $cache[$username]['lastcheck'] = time();
    }
    else {
    $cache[$username]['lastcheck'] = time()+60;
    }

    update_option( 'rarst_twitter_user', $cache );
    }

    if ( false != $display )
    echo $cache[$username][$field];
    return $cache[$username][$field];
    }
❤️由托管WPCode
在WordPress中一键使用

用法

一旦你粘贴了函数,现在你就可以在任何你喜欢的WordPress模板文件中使用代码了。只需粘贴以下代码:

echo rarst_twitter_user('wpbeginner', 'name').' has '.
rarst_twitter_user('wpbeginner', 'followers_count').' followers after '.
rarst_twitter_user('wpbeginner', 'statuses_count').' updates.';
❤️由托管WPCode
在WordPress中一键使用

上面的代码将显示如下所示:

塔克网在1300次更新后拥有5846名追随者。

来源:RARST

标签:数量更多如何在推特WordPress


WordPress教程排行
最近发表
网站分类
标签列表