• 関数外で使いたい変数があるので関数内でグローバル宣言して呼び出しているのですが、反映されないようです。

    $url=get_permalink();
    $title=’ページタイトル’;

    function tw_count() {
    global $url ;
    $json = @file_get_contents(‘http://urls.api.twitter.com/1/urls/count.json?url=’ .$url. ”);
    $array = json_decode($json,true);
    $tw_count = $array[‘count’];
    echo $tw_count;
    echo $url;
    }

    一番最後のecho $url;は確認用のテスト記述なので関係ありません。

    $url=get_permalink();
    $title=’ページタイトル’;

    を関数内に持ってくると反映されるのですが考えられる原因をアドバイスいただけましたら非常に助かります。

4件の返信を表示中 - 1 - 4件目 (全4件中)
  • こんばんわ、karisumasaking48さん

    Post ID を渡してみてはいかがでしょうか?

    get_permalink( $post->ID );

    個人的には global ではなく、tw_count() に引数を付けて上げたほうがいいような気がします。
    あとは、file_get_contents ではなく wp_remote_get() の使用をオススメします。

    /**
     * Get Twitter count.
     *
     * @param  intval $post_id
     * @return intval
     */
    function get_twitter_count( $post_id = 0 ) {
        global $post;
        $post_id = ( (int)$post_id > 0 ) ? (int)$post_id : (int)$post->ID;
        $url     = ( get_permalink( $post_id ) !== false ) ? get_permalink( $post_id ) : false;
        $result  = get_transient( 'twitter_count_' . $post_id );
    
        if ( $url !== false && $result === false ) {
            $response = wp_remote_get( 'http://urls.api.twitter.com/1/urls/count.json?url=' . rawurlencode( $url ) . '&callback=?' );
            if ( ! is_wp_error( $response ) && $response['response']['code'] === 200 ) {
                $response = json_decode($response['body']);
                $result   = (int)$response->count;
                set_transient( 'twitter_count_' . $post_id, (int)$result, 60 * MINUTE_IN_SECONDS );
            }
        }
    
        return (int)$result;
    }

    こんな感じですかね。
    未テストなのでちゃんと動くか分かりません、はい。

    トピック投稿者 karisumasaking48

    (@karisumasaking48)

    回答ありがとうございます。

    実はまだ作成中の関数があるのです。

    //—————————————————————————
    // SNSのシェア数を取得
    //—————————————————————————
    //●twitter
    function tw_count() {
    $url = get_permalink();
    $json = @file_get_contents(‘http://urls.api.twitter.com/1/urls/count.json?url=’ .$url. ”);
    $array = json_decode($json,true);
    $tw_count = $array[‘count’];
    echo $tw_count;
    }
    add_shortcode(‘tw_count’, ‘tw_count’);

    //●Facebook
    function fb_count() {
    $url = get_permalink();
    $json = @file_get_contents(‘http://graph.facebook.com/?id=’ . $url . ”);
    $array = json_decode($json,true);
    if(!isset($array[‘shares’])){
    $count = 0;
    }else{
    $count = $array[‘shares’];
    }
    $fb_count = $count;
    echo $fb_count;
    }
    add_shortcode(‘fb_count’, ‘fb_count’);

    //●hatena
    function hb_count() {
    $url = get_permalink();
    $count = @file_get_contents(‘http://api.b.st-hatena.com/entry.count?url=’ . $url . ”);
    if(!isset($count) || !$count){
    $count = 0;
    }
    $hb_count = $count;
    echo $hb_count;
    }
    add_shortcode(‘hb_count’, ‘hb_count’);

    //●Google+
    function gp_count() {
    $url = get_permalink();
    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_URL, “https://clients6.google.com/rpc?key=AIzaSyCKSbrvQasunBoV16zDH9R33D88CeLr9gQ” );
    curl_setopt( $ch, CURLOPT_POST, 1 );
    curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
    curl_setopt( $ch, CURLOPT_POSTFIELDS, ‘[{“method”:”pos.plusones.get”,”id”:”p”,”params”:{“nolog”:true,”id”:”‘ . $url . ‘”,”source”:”widget”,”userId”:”@viewer”,”groupId”:”@self”},”jsonrpc”:”2.0″,”key”:”p”,”apiVersion”:”v1″}]’ );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch, CURLOPT_HTTPHEADER, array( ‘Content-type: application/json’ ) );
    $result = curl_exec( $ch );
    curl_close( $ch );
    $obj = json_decode( $result, true );
    if(!isset($obj[0][‘result’][‘metadata’][‘globalCounts’][‘count’])){
    $count = 0;
    }else{
    $count = $obj[0][‘result’][‘metadata’][‘globalCounts’][‘count’];
    }
    $gp_count = $count;
    echo $gp_count;
    }
    add_shortcode(‘gp_count’, ‘gp_count’);

    //●Feedly
    //注) get_bloginfo(‘rss2_url’)はリンクエンドに/を含まない
    function fl_count() {
    $feed = rawurlencode(get_bloginfo(‘rss2_url’));
    $json = @file_get_contents(“http://cloud.feedly.com/v3/feeds/feed%2F{$feed}”);
    $obj = json_decode($json,true);
    if(isset($obj[‘subscribers’])){
    $fl_count = $obj[‘subscribers’];
    }else{
    $fl_count = 0;
    }
    echo $fl_count;
    }
    add_shortcode(‘fl_count’, ‘fl_count’);

    //●Youtube
    function yt_count() {
    $channel = ‘あなたのyoutubeチャンネル名に書き換えてください’;
    $json = @file_get_contents(“http://gdata.youtube.com/feeds/api/users/{$channel}?alt=json”);
    $obj = json_decode($json,true);
    $yt_count = $obj[‘entry’][‘yt$statistics’][‘subscriberCount’];
    echo $yt_count;
    }
    add_shortcode(‘yt_count’, ‘yt_count’);

    //●Linkedin
    function li_count() {
    $url = get_permalink();
    $json = @file_get_contents(‘http://www.linkedin.com/countserv/count/share?url=’ .$url. ”);
    $json = rtrim($json,’);’);
    $json = ltrim($json,’IN.Tags.Share.handleCount(‘);
    $array = json_decode($json,true);
    $li_count = $array[‘count’];
    echo $li_count;
    }
    add_shortcode(‘li_count’, ‘li_count’);

    KUCKLUさんが回答して頂いた記述が理解できれば、ほかのものも作り変えることも可能なのですが、私はまだその領域に達していないので少々難しいようです。。。

    ちなみに上記関数を作成した上で

    <?ぴーえいちぴー tw_count() ?>

    などと個別に表示させることはもちろん

    <?ぴーえいちぴー
    $all_count = tw_count() + fb_count() + hb_count() + gp_count()
    if($all_count >= 200) {
    echo ‘認定記事’
    }
    ?>

    というように指定した任意の数をシェア数が上回ったら色々したいのですが、

    <?php
    $all_count = tw_count() + fb_count();
    echo $all_count ;
    ?>

    とか

    <?php
    $tw_count = tw_count();
    $fb_count = fb_count();
    $all_count = ( $tw_count + $fb_count );
    ?>

    とかいろいろ試しているのですが計算されずに文字列の足し算みたいな出力になっているのでそこで苦戦しています。

    動作確認用の記事です。
    http://amami-city.xsrv.jp/ozin/%E3%83%86%E3%82%B9%E3%83%88%E8%A8%98%E4%BA%8B-11-2-2-2-2-2-2-2-2-2-2-2-2-2

    関数はなんとなく分かってきたというレベルですので(*´ー`*)

    関数なんか作らずに全部、index.phpで記述するならできますが、あまり記述を増やしたくないので、できればfanctions.php内で関数として定義付けて呼び出すという形で記述を減らしたいのです。

    独自関数を作る前はSNS Count Cacheという各SNSのシェア数を取得し任意の指定した場所に表示してくれるプラグインがあるのですが、もしこのプラグインが廃止や停止になった場合、このプラグインに属するphp記述をしていた時はサイトがエラーを返すので、それならいっそのことシェア数取得する関数作ってしまえとう経緯からです(*´ー`*)

    こんばんわ、karisumasaking48さん

    僕が先ほど Gist に上げたものです。
    参考になれば幸いです。

    [ WordPress ] 記事のシェア数を取得し表示する。
    https://gist.github.com/kuck1u/abad609195c29bbc9bd2

    上記の関数達は引数にポスト ID を渡せるようになっていますが、ポスト ID を渡さなくても大丈夫です。引数がなければ、現在のページ ID を見るようにしています。

    トピック投稿者 karisumasaking48

    (@karisumasaking48)

    KUCKLUさん

    php…奥が深いですね(*´ー`*)
    ありがとうございます!!!

4件の返信を表示中 - 1 - 4件目 (全4件中)
  • トピック「global宣言が無効になっている」には新たに返信することはできません。