4

I'm using Dukt's very nice Twitter plugin and it's working nicely returning the most recent tweet as I want, however I cannot seem to access the tweet's image. Is this possible?

{% set tweets = craft.twitter.get("statuses/user_timeline.json?screen_name=#{twitterHandle}&count=1&exclude_replies=true&include_rts=true&include_entities=true") %}

I've tried adding &include_entities=true

When I print out the json, I can't seem to see any media URLs. Has anyone else had any joy here? Thanks

carlcs
  • 36,220
  • 5
  • 62
  • 139
  • Hmm digging around further using Twitter's test console https://dev.twitter.com/rest/tools/console seems maybe the images I'm seeing in the tweets were only coming from the client embedding a URL which was autounfurled in Twitter – André Goldstein Oct 17 '16 at 15:22
  • Has anyone had issues with this only working sporadically? I have a feed where images show up on some tweets but not others. Any help would be greatly appreciated. – SteveO Feb 08 '18 at 18:38
  • This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. You can also add a bounty to draw more attention to this question. - From Review – Robin Schambach Feb 10 '18 at 15:29

1 Answers1

4

There should actually be a entities.media property available in the JSON, when there are images attached to the tweet.

This is what works for me:

{% set tweets = craft.twitter.get('statuses/user_timeline') %}

{% for tweet in tweets %}
    <li>
        <p>{{ tweet.text }}</p>

        {% if tweet.entities.media is defined %}
            {% for item in tweet.entities.media if item.type == 'photo' %}
                <img src="{{ item.media_url }}">
            {% endfor %}
        {% endif %}
    </li>
{% endfor %}
carlcs
  • 36,220
  • 5
  • 62
  • 139
  • Brilliant thanks Carl, this is what I was starting to get to by testing different profiles. Turns out this doesn't cover images that have come from them embedding a link (I don't think), I may revert to directly embedding the default card style it's pretty nice these days – André Goldstein Oct 17 '16 at 15:58
  • Ahh yeah, I don’t think Twitter Cards images are available form the API. – carlcs Oct 17 '16 at 16:17
  • Thanks André, I posted a similar question [http://craftcms.stackexchange.com/questions/17059/twitter-api-request-for-images-with-dukt-twitter] and couldn't work out how to pickup the media URLs - let us know what you ended up doing, perhaps with an example as the Plugin's docs are rather succinct. – Nutmeg Oct 18 '16 at 05:44
  • @nutmeg the Twitter API returns all media with the entities property. What kind of data are you missing? Usual links to websites that are extended as Twitter Cards? – carlcs Oct 18 '16 at 06:21
  • Has anyone else had an issue where long tweets, that include an image, are being truncated, and not showing the image? Any help on this would be greatly appreciated. Here is an example of two tweets with the same image http://yak.la/temp/jm.png – SteveO Feb 21 '18 at 00:09
  • 1
    I solved my issue. You must use the api parameter: tweet_mode:'extended', then call {{tweet.full_text|autoLinkTweet}} – SteveO Feb 21 '18 at 05:18
  • @SteveO nice! Might want to post a dedicated answer for it? – carlcs Feb 21 '18 at 06:27
  • @SteveO Do you mind sharing your code? – Nutmeg Apr 17 '18 at 04:05
  • Where would you add the api parameter tweet_mode:'extended'? – Nutmeg Aug 14 '18 at 09:01