【问题标题】:Wordpress Shortcode to get Youtube Channel Views via YouTube APIWordpress 简码通过 YouTube API 获取 Youtube 频道浏览量
【发布时间】:2017-12-13 04:59:53
【问题描述】:

我是 Wordpress 用户,而不是程序员。 我想在 functions.php 中创建一个简码来获取任何 youtube 频道的总观看次数。

我使用简码并提供频道 ID 值。在短代码函数通过 YouTube API 回馈给定频道 ID 的当前频道观看次数后。

在 YouTube 文档中,我找到了以下代码:

function channelsListById($service, $part, $params) {
    $params = array_filter($params);
    $response = $service->channels->listChannels(
        $part,
        $params
    );

    print_r($response);
}

channelsListById($service,
    'statistics', 
    array('id' => 'UC_x5XG1OV2P6uZZ5FSM9Ttw'));
}

响应看起来像这样:

/**
 * API response
 */
{
  "kind": "youtube#channelListResponse",
  "etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/d9WwK8U1FuV6c-shZpzmnYUgGvQ\"",
  "pageInfo": {
    "totalResults": 1,
    "resultsPerPage": 1
  },
  "items": [
    {
      "kind": "youtube#channel",
      "etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/Psv3E9LCdk66gkF4HSIYK44crMI\"",
      "id": "UC_x5XG1OV2P6uZZ5FSM9Ttw",
      "statistics": {
        "viewCount": "118283693",
        "commentCount": "393",
        "subscriberCount": "1165598",
        "hiddenSubscriberCount": false,
        "videoCount": "4435"
      }
    }
  ]
}

我想,我需要添加一些代码来获取项目字段“viewCount”的值。

我不知道是否可以这样做,但我希望你们中的一些专业人士知道解决方案。

在代码的最后,我只需要添加:

add_shortcode( 'channel_views', 'shortcode_title' );

如果你能用我必须反转为functions.php的代码来回应,那就太棒了;)

如果您需要更多信息才能回复,尽管问我。

【问题讨论】:

    标签: php wordpress youtube youtube-api


    【解决方案1】:

    使用这个插件。

    https://wordpress.org/plugins/youmax-channel-embeds-for-youtube-businesses/

    这个插件只是简单地从 youtube 频道以网格格式获取所有视频。

    【讨论】:

    • 这个插件不符合 OP 的要求。
    猜你喜欢
    • 2015-03-27
    • 2019-10-06
    • 2022-12-24
    • 1970-01-01
    • 2020-11-26
    • 2012-04-05
    • 2022-11-07
    • 1970-01-01
    • 2020-10-26
    相关资源
    最近更新 更多