Recent Posts with Pictures (take 1)

If I remember correctly, I used a recent post widget developed by beautifulbeta. It’s a great widget (think they call it gadgets now), but I wanted to have like a picture in it to… like the first picture in the post. So I kinda added stuff to their javascript code. While I was in there, I added a few other things too. Well, in addition to having pictures, you can choose to ignore a number of your most recent blog posts. For example, ignore the latest 3 (or something like that) by setting the variable numpoststohide. In your layout, add a html/js widget and copy-paste the code below. Don’t forget to put in YOURBLOG.COM.

<script style="text/javascript" src="http://recentPostsWithFirstPic.js"></script><script style="text/javascript">var numposts = 5;var showpostdate = false;var showpostsummary = true;var numchars = 100;var standardstyling = true;var numpoststohide=5;</script><script src="http://YOURBLOG.COM/feeds/posts/default?orderby=published&alt=json-in-script&callback=showrecentposts"></script>

By the way, this is a pretty long post with some javascript and css sprinkled in. ^^
Here’s the javascript. I don’t have any place host it for you, so please host it where you can.

function showrecentposts(json){for (var i = numpoststohide; i < numposts+numpoststohide; i++){  var entry = json.feed.entry[i];  var posttitle = entry.title.$t;  var posturl;  if (i == json.feed.entry.length) break;  for (var k = 0; k < entry.link.length; k++) {   if (entry.link[k].rel == 'alternate') {    posturl = entry.link[k].href;    // get the postcontent    // if the Blogger-feed is set to FULL, then the content is in the content-field    // if the Blogger-feed is set to SHORT, then the content is in the summary-field    if ("content" in entry) var postcontent = entry.content.$t;    else if ("summary" in entry) var postcontent = entry.summary.$t;    else var postcontent = "";

    // For getting the first photo.    var firstPic = /<img \S[^>]*>/;    if(postcontent.match(firstPic)) var firstPicURL = postcontent.match(firstPic);    else firstPicURL = 'nothing';    var gotPic;    // format firstPicURL attributes    var width = /width="\S\S\S"/;    var height = /height="\S\S\S"/;    if(firstPicURL.toString().match(width)){     firstPicURL = firstPicURL.toString().replace(width, 'width="200"');     firstPicURL = firstPicURL.toString().replace(height, " ");     gotPic = true;    } else if(firstPicURL.toString().match('BLOGGER_PHOTO_ID')){     // blogger pics     firstPicURL = firstPicURL.toString().replace("/>", 'width="200" />');     gotPic = true;    } else gotPic = false;    break;   }  }  posttitle = posttitle.link(posturl);

  var readmorelink = "(more)";  readmorelink = readmorelink.link(posturl);  var postdate = entry.published.$t;  var cdyear = postdate.substring(0,4);  var cdmonth = postdate.substring(5,7);  var cdday = postdate.substring(8,10);  var monthnames = new Array();  monthnames[1] = "Jan";  monthnames[2] = "Feb";  monthnames[3] = "Mar";  monthnames[4] = "Apr";  monthnames[5] = "May";  monthnames[6] = "Jun";  monthnames[7] = "Jul";  monthnames[8] = "Aug";  monthnames[9] = "Sep";  monthnames[10] = "Oct";  monthnames[11] = "Nov";  monthnames[12] = "Dec";  if ("content" in entry) var postcontent = entry.content.$t;  else if ("summary" in entry) var postcontent = entry.summary.$t;  else var postcontent = "";  var re = /<\S[^>]*>/g;   postcontent = postcontent.replace(re, "");  if (!standardstyling) document.write('<div class="bbrecpost">');  if(gotPic == true) document.write('<a href="' + posturl + '">' + '<div class="crop">' + firstPicURL + '</div></a>');  document.write('<br>');  document.write(posttitle);  if (showpostdate == true) document.write(' - ' + monthnames[parseInt(cdmonth,10)] + ' ' + cdday);  if (!standardstyling) document.write('</div><div class="bbrecpostsum"">');  if (showpostsummary == true){   if (standardstyling) document.write('<br/>');   if (postcontent.length < numchars) {    if (standardstyling) document.write('<i>');    document.write(postcontent);    if (standardstyling) document.write('</i>');   } else {    if (standardstyling) document.write('<i>');    postcontent = postcontent.substring(0, numchars);    var quoteEnd = postcontent.lastIndexOf(" ");    postcontent = postcontent.substring(0,quoteEnd);    document.write(postcontent + '...' + readmorelink);    if (standardstyling) document.write('</i>');   }  }  if (standardstyling) document.write('<br/>');  if (!standardstyling) document.write('</div>');  if (standardstyling) document.write('<br/>'); }}

One more thing. ^^. I used some code for cropping pictures. Please add

/* For cropping */div.crop {   height: 90px;   width: 90px;   overflow: hidden;   margin-left: auto;   margin-right: auto;}div.crop img {   margin: -40px 0 0 -50px;}

to the header of your html template file. Near all the other css styles. Anywhere between <b:skin><![CDATA[ and ]]></b:skin> should be ok.

When everything is done properly, it should look something like this.

I’m not an expert at javascript or css. So writing all these from scratch is a little beyond my reach in terms of time constraints. So I’ve taken the easy way out. I’ve took what was there and pieced them together whilst adding a little in return.

Anyway, I thought this might be useful since there are lots of sites with recent posts widgets, but I’ve yet to see one with pictures included. It works for blogger. I don’t know about wordpress etc. Anyone care to test it out for me?

Related posts:

  1. Problems opening in IE7 fixed! I had the IE woes. This site would open in...
  2. Blog Update I’ll be changing some elements in this blog over...
  3. I survived! Or rather my posts survived the move! I lost...

Related posts brought to you by Yet Another Related Posts Plugin.

6 Responses to “Recent Posts with Pictures (take 1)”

  1. Otaku Dan Says:

    may i please use that code?

    [Reply]

  2. k Says:

    @otaku-dan, go ahead. ^^. let me know how you went.

    [Reply]

  3. Sandy-Kun Says:

    Nice Hack!! ^^ I’ll try this!! :D
    But..One question (or more?!)
    Can it list my 20 latest post?? :p

    [Reply]

  4. k Says:

    @sandy-kun, yeap you can. look in the first scroll box above. you can find something like numpost=5 and numposttohide=5. just change the first ‘5′ to ‘20′ and the second ‘5 to ‘0′. this will list your 20 latest posts. the maximum you can go with blogger is 25. that’s the maximum number of entries in the JSON feed i think. ^^

    [Reply]

  5. Sandy-Kun Says:

    Thank you k… :D
    I’ll try this out so…
    and then, second question… How to put the news item that aren’t shown in our main page??
    (i just figure that your recent comment is not in the main page, but in the past page…)

    [Reply]

  6. k Says:

    @sandy-kun, you mean recent posts? just set the variable numpoststohide (spell miss in my previous comment, forgot the ’s’ in posts). if you have 3 posts in your main page, set this variable to 3 etc. this will make sure that 3 most recent posts will not show up in the widget. let me know if you have any problems ^^

    [Reply]

Follow comments in this post with a RSS 2.0 feed.

Leave a Reply




Free manga (1)

(read more)

If you’re tight on cash and don’t mind reading second hand manga… and completely ok with going through and picking up what people throw away, [...]

Hobbycon 6, the naughty bits (2)

(read more)

The title says it all. Only the naughty bits. So, it’s definitely NSFW and not something you would like to show to your grandma. ^^. [...]

Christmas love in the otaku world (2)

(read more)

With christmas just round the corner, there were quite a number of cute figures in (and trying to get out of) santa suits at hobbycon. [...]

Shibuya Bape Store (6)

Don’t own any Bape stuff. Cool but find them a little way too much on the overpriced side. Shibuya has this flagship Bape store. Do [...]

Merry Christmas (2)

It’s Christmas Eve already! Time really flies when you’ll looking back. Anyone still doing any last minute shopping? I bought my last present on amazon [...]

Shibuya - the dodgy area and Kendo gear (11)

If you’ve been to Tokyo, or planning a trip here, Shibuya will probably be on your list… right? hmmm… bright neon lights!

Have you been to [...]

Sea of taxis in Sendai (2)

Sendai is a beautiful city. Lots of green, lots of beautiful girls and lots of beef tongue set meals. ^^. Although not as big a [...]

Angel Road 天使の散歩道 (3)

This is a place I would like to go in 2009. And it seems like I might already have a chance to go there in [...]

Banraiken 2nd stage ramen (3)

The ramen yokocho is probably a must for visitors in Sapporo (often called the city of a thousand ramen). Walking through the alley, there is [...]