• 解決済 tenten23

    (@tenten23)


    過去記事を五分毎に画像付きツイートをするためにFunction.phpに以下のコードを入れています。
    ツイートに使う画像はカスタムフィールドの画像を取得しようとしているのですが、うまく取得できませんでした。
    $attachment_idに直接IDを入れるとツイートに成功するので他は大丈夫だと思います。
    カスタムフィールドはadvanced custom fields の画像(IDもURLも試しました)を使って作っています。質問内容はadvanced custom fieldsの画像IDの取得方法?の内容がメインになりそうなので、プラグインのところで質問しようかと思いましたが、function.phpに色々書き込む点とget post metaなどに変更する必要もあるかも?という点からこちらに投稿させて頂きました。

    宜しくお願い致します。

    require_once 'tmhOAuth.php';
    function tweetPost(){
        //トークンなどの設定
        $tmhOAuth  = new tmhOAuth(array(
            "consumer_key" => 'キー',
            "consumer_secret" => 'シークレットキー',
            "token" =>'トークン',
            "secret" => 'シークレットトークン',
            'curl_ssl_verifypeer' => false //SSL対応に必要
        ));
    
        //ツイートする記事の取得
        $post = get_posts(array(
            'numberposts' => 1,
    'post_type' => 'post-tweet',
            'orderby' => 'rand'
        ));
    
        //画像情報取得
    
    $attachment_id = get_field("tweet-image", $post->ID);
    //$attachment_id = 1396;
    $file = wp_get_attachment_image_src($attachment_id, 'full');
    $file = $file[0];
    
            $image = file_get_contents( $file );
            $imagesize = getimagesize( $file );
    
            $params = array(
                'status' =>  mb_strimwidth('' .strip_tags($post[0]->post_content), 0, 116, "...") . ''. get_the_title($post[0]->ID),
                'media[]' => $image . ";type=" . $imagesize['mime'] . ";filename=" . basename( $file )
            );
    
            $code = $tmhOAuth->request(
                'POST',
                'https://api.twitter.com/1.1/statuses/update_with_media.json',
                $params,
                true,
                true
            );
    
    }
    
    //Cronへ登録
    add_action('tweetcron', 'tweetPost');
    function my_activation() {
    if ( !wp_next_scheduled( 'tweetcron' ) ) {
        wp_schedule_event(time(), '5m', 'tweetcron');
        }
    }
    add_action('wp', 'my_activation');
2件の返信を表示中 - 1 - 2件目 (全2件中)
  • get_posts の返値は、配列になります。

    トピック投稿者 tenten23

    (@tenten23)

    ものすごい的確なアドバイスありがとうございます!
    おかげさまで解決しました!

2件の返信を表示中 - 1 - 2件目 (全2件中)

トピック「過去記事を画像付きでツイートするには」には新たに返信することはできません。