Posted By

Rembrand on 08/05/10


Tagged

blog tumblr


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

lolindirfaelivrin
fhuel
kantiano
Priestd09


Embed Tumblr posts into website


 / Published in: PHP
 

URL: http://stocklamp.tumblr.com/post/274675902/putting-your-tumblr-posts-on-your-websites-the-easy-way

Embed Tumblr posts the easy way. Import the xml, split by post-type and seperate the different types of content. I had a hard time finding an easy way to do this and ended up mixing and matching info from 2 very helpful places (check the url\'s in the src) and a co-worker who\'s better versed in PHP than me.

  1. <?php
  2. //http://stocklamp.tumblr.com/post/274675902/putting-your-tumblr-posts-on-your-websites-the-easy-way
  3. //http://finlay.tumblr.com/post/529010691/embed-tumblr-into-your-website
  4. //http://www.tumblr.com/docs/en/api#api_read
  5.  
  6. $xml = simplexml_load_file('http://webretailcompany.tumblr.com/api/read/xml?num=3');
  7. $posts = $xml->xpath("/tumblr/posts/post");
  8.  
  9. foreach($posts as $post) { ?>
  10.  
  11. <div class="post post-<?php echo $post['type'] ?>">
  12. <div class="post-date"><a href="<?php echo $post['url-with-slug']; ?>"><?php echo date("jS D M, H:i",strtotime($post['date'])); ?></a></div>
  13.  
  14. <?php if ($post['type'] == 'regular') { ?>
  15. <div class="post-title" id="post-<?php echo $post['id'];?>"><a href="<?php echo $post['url-with-slug']; ?>"><?php echo $post->{'regular-title'}; ?></a></div>
  16. <?php } ?>
  17.  
  18. <?php if ($post['type'] == 'conversation') { ?>
  19. <div class="post-title" id="post-<?php echo $post['id'];?>"><a href="<?php echo $post['url-with-slug']; ?>"><?php echo $post->{'conversation-title'}; ?></a></div>
  20. <?php } ?>
  21.  
  22. <div class="post-body">
  23.  
  24. <?php if ($post['type'] == 'regular') {
  25.  
  26. $small_post = substr($post->{'regular-body'},0,539); ?>
  27.  
  28. <?php echo $small_post . ' &hellip;'; } ?>
  29.  
  30. <?php if ($post['type'] == 'quote') { ?>
  31. <?php echo $post->{'quote-text'}; ?>
  32. <?php echo $post->{'quote-source'}; ?>
  33. <?php } ?>
  34.  
  35. <?php if ($post['type'] == 'photo') { ?>
  36. <?php echo $post->{'photo-caption'}; ?>
  37. <?php echo $post->{'photo-set'}; ?>
  38. <?php echo $post->{'photo-url-500'}; ?>
  39. <?php echo $post->{'video-source'}; ?>
  40. <?php echo $post->{'video-player'}; ?>
  41. <?php } ?>
  42.  
  43. <?php if ($post['type'] == 'link') { ?>
  44. <?php echo $post->{'link-text'}; ?>
  45. <p><a href="<?php echo $post->{'link-url'}; ?>"><?php echo $post->{'link-url'}; ?></a>
  46. <?php echo $post->{'link-description'}; ?>
  47. <?php } ?>
  48.  
  49. <?php if ($post['type'] == 'conversation') { ?>
  50. <?php echo $post->{'conversation-text'}; ?>
  51. <?php } ?>
  52.  
  53. <?php if ($post['type'] == 'video') { ?>
  54. <?php echo $post->{'video-caption'}; ?>
  55. <?php echo $post->{'video-source'}; ?>
  56. <?php echo $post->{'video-player'}; ?>
  57. <?php } ?>
  58.  
  59. <?php if ($post['type'] == 'conversation') { ?>
  60. <?php echo $post->{'audio-caption'}; ?>
  61. <?php echo $post->{'audio-player'}; ?>
  62. <?php echo $post->{'audio-plays'}; ?>
  63. <?php } ?>
  64.  
  65. </div><!-- //end post-body -->
  66.  
  67. <p><a class="more" href="<?php echo $post['url-with-slug']; ?>">&raquo; Read more</a></p>
  68. </div><!-- //end post-->
  69.  
  70. <?php } ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: fhuel on October 5, 2010

Just what I was looking for, great stuff! Thank you for sharing

Posted By: gamma888 on October 5, 2010

Thank you for sharing this little hidden gem. Why doesn't the photos or videos appear when they are being uploaded to Tumblr's server? It seems like photos or videos (eg. Youtube video) only appear... not the embedded ones. Any ideas?

Again, thank you for sharing!!!

Posted By: gamma888 on October 5, 2010

Thank you for sharing this little hidden gem. Why doesn't the photos or videos appear when they are being uploaded to Tumblr's server? It seems like photos or videos (eg. Youtube video) only appear... not the embedded ones. Any ideas?

Again, thank you for sharing!!!

Posted By: gamma888 on October 5, 2010

Thank you for sharing this little hidden gem. Why doesn't the photos or videos appear when they are being uploaded to Tumblr's server? It seems like photos or videos (eg. Youtube video) only appear... not the embedded ones. Any ideas?

Again, thank you for sharing!!!

Posted By: gamma888 on October 5, 2010

Thank you for sharing this little hidden gem. Why doesn't the photos or videos appear when they are being uploaded to Tumblr's server? It seems like photos or videos (eg. Youtube video) only appear... not the embedded ones. Any ideas?

Again, thank you for sharing!!!

Posted By: Rembrand on December 7, 2010

@gamma888: Hm, no idea. If it’s visible in tumblr, it should work after import as well. Haven’t come across the problem myself.

Posted By: dgovil on April 21, 2011

I can confirm that Tumblr photo URLs are not resolving properly, but I can't figure out why. One slightly messy solution that does work however, is to change

to

Posted By: dgovil on April 21, 2011

Meant to say changing the photo-url-500 to simply photo-url and putting it as an iframe src. It works, if not very elegantly.

Photo-url-500 can't seem to resolve the media link that Tumblr uses.

Maybe Rembrand can shed some like on this, but if I find a fix, I'll post back.

You need to login to post a comment.