0
我正在使用此代碼顯示共享計數器;如何在wordpress中爲share share計算添加post meta?
function facebook_share() {
$post_id = get_the_ID();
$url = get_permalink($post_id);
$response = wp_remote_get('http://graph.facebook.com/?id=' . $url);
if(!is_wp_error($response)) {
$json = json_decode(wp_remote_retrieve_body($response));
return isset($json->shares) ? $json->shares : 0;
} else {
return 0;
}
}
function googleplus_share() {
$post_id = get_the_ID();
$url = get_permalink($post_id);
$response = wp_remote_post('https://clients6.google.com/rpc', array(
'body' => '[{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":"' . $url . '","source":"widget","userId":"@viewer","groupId":"@self"},"jsonrpc":"2.0","key":"p","apiVersion":"v1"}]',
'headers' => array(
'content-type' => 'application/json'
)
));
if(!is_wp_error($response)) {
$json = json_decode(wp_remote_retrieve_body($response));
if(!isset($json[0]->error)) {
return $json[0]->result->metadata->globalCounts->count;
} else {
return 0;
}
} else {
return 0;
}
}
function social_shares() {
$fb = facebook_share();
$google = googleplus_share();
$totalcounts = $fb + $google;
return $totalcounts;
}
,但它太慢了,因爲總是在計算總份額,所以我想總股份數爲保存在數據庫後元 ......然後,我將展示總份額的數量從數據庫
感謝答案
嗨@Simon謝謝你的回答,我可以使用高級自定義字段插件嗎? –
我相信是這樣的,我自己並沒有使用這個插件,但是你可以保存自定義的數據與它的帖子。然而,我個人會說,你不需要它,因爲你已經有了你的社交數據代碼,你可以直接進入該代碼。你只需要我給你看的兩個功能,ACF自己會使用它。 –
我需要使用add_post_meta嗎? –