Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save blindcyclistsunion/1011001 to your computer and use it in GitHub Desktop.
Save blindcyclistsunion/1011001 to your computer and use it in GitHub Desktop.
Tumblr recent posts widget in javascript
<!-- need jquery for this, this line should really go in your theme html
just after the <head> marker, might work OK here if you aren't already using jquery -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<!-- Mimic the style of the tumblr headers from your theme -->
<div class="heading">Recent Posts</div>
<!-- The javascript below will insert your recent posts lists in here -->
<div id="recentPosts"></div>
<!-- // when this script is run, it sets a variable named tumblr_api_read to the result -->
<script type="text/javascript" src="http://blindcyclistsunion.tumblr.com/api/read/json"></script>
<!-- // this is the script that builds the html to go in the div above -->
<script type='text/javascript'>
// number of posts to fetch
const recent_posts_count = 10;
// maximum length of titles for truncation (plus 3 for ...)
const truncate_length = 70;
// start a div for the recent posts, you can add any ul,etc, formatting here also
var recentPostsHtml = "<div>";
// loop around and proess recent_post_count number of recent posts
for (var i = 0; i < recent_posts_count; i++) {
// get a post
var post = tumblr_api_read.posts[i];
// we need to extract different fields for different kinds of post
var extract_field = "";
// we may wish to truncate some (or all, or none) of them
var truncate = false;
// we may wish to add a prefix to indictae (for example) the kind of post
var prefix = "";
// In order for this to work really properly, we need to cater for all
// the different post types that are available ...
// regular 'text' post
if( post.type == "regular")
{
prefix = "";
truncate = false;
extract_field = "regular-title";
}
// link post
if( post.type == "link")
{
prefix = "[Link] ";
truncate = true;
extract_field = "link-text";
}
// quote post
if( post.type == "quote")
{
prefix = "[Quote] ";
truncate = true;
extract_field = "quote-text";
}
// photo post
if( post.type == "photo")
{
prefix ="[Photo] ";
truncate = true;
// note we use 'slug' as the field here, because tumblr provides us with
// a nice truncated short clean summary. whereas the captions have html in them
// html like div an p, which will muck up the formatting
extract_field = "slug";
}
// Video post
if( post.type == "video")
{
prefix = "[Video] ";
truncate = true;
// slug again, see note above
extract_field = "slug";
}
// Audio post
if( post.type == "audio")
{
prefix = "[Audio] ";
truncate = true;
// slug again, see note above
extract_field = "slug";
}
// Conversation post
if( post.type == "conversation")
{
prefix = "[Chat] ";
truncate = true;
extract_field = "conversation-title";
}
// Ask/Answer post
if( post.type == "answer")
{
prefix = "[Answer] ";
truncate = true;
// might need to be 'slug' as well
extract_field = "answer";
}
// get the post's url
var url = post.url;
// get the field we're going to use as the title
var title_field = $.trim( post[extract_field] );
// truncate and add elipses if desired
if( title_field.length > truncate_length )
{
title_field = title_field.substring( 0, truncate_length ) + "...";
}
// add the prefix is there is one
var title = prefix + title_field;
// add it into the html string
recentPostsHtml += '<p><a href="' + url + '">' + title + '</a></p>';
}
recentPostsHtml += '</div>';
$("#recentPosts").append(recentPostsHtml);
</script>
@dddaaannnTemp
Copy link

Thank you that is a better answer! Because i tries tweaking the for-loop to skip photo posts, then increase i by one to compensate for the 'wasted' loop. (i still want 10 posts so it has to redo the loop. but of course the post[i] has to be the next value so i used two variables. one controlling the condition and one controlling the post[i]. i ended up having a for loop with two variables. which should work fine and i t made sense to me but javascript didnt like the variable++; inside the forloop. it just didnt work. and besides to accumilate a number of 10 text posts the script would have to analyze more than the last 50 posts (maximum)... so yeah :) thanks so much for your help :) the script is very well documented and i could understand what it does by looking at it. (i used to do amateur java programming some time ago).

@ChristineWilde
Copy link

Thank you so much for this! Been looking for weeks for something like this.

One question though... How do I add a style to the div so each is styled differently for each post type? I want to include my little post icons to the Latest Posts list.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment