Embed Tumblr posts into website


/ Published in: PHP
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  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 } ?>

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.