Display "Post Titles" using Blogger JSON Feed |
Display "Post Titles" using Blogger JSON Feed Posted: 23 Oct 2015 07:35 AM PDT In Part#4 we discussed how to extract "Labels List" data from Blogger JSON feed API and print this list with hyperlinks. Today we will extract Post Titles from BlogSpot JSON feed by parsing the data in JavaScript, in other words we are creating the "Recent Posts widget" that will only display titles. Exact same approach and technique will be used. We are trying to make sure we discuss data extraction in simple steps through various tutorials before you could actually build a useful widget like a "recent posts widget" that could display Titles, Labels, Comment count, Thumbnail image and timestamp. Lets get straight to work! Note: Click the Button below to see full list of topics under discussion. Topics List Where is "Title String" and "Title URL" stored?The Post Title String is stored inside the [ ] entry array followed by { } title object node as shown in the screenshot below.
Which means:
To extract the Title Link or URL we will follow this path:
As you can observe the title href attribute is located inside the array [ ] link and on its 4th ID i.e.{ } 4. Since it is not important that href value might exist inside the fourth array element always therefore we will make sure to extract href value of only that node which contains the rel: "alternate" name/value pair. Since [ ] link node is an array itself that is why we expressed it as link[j] so to make sure which element ID of link are we accessing. I hope you now know where each data exists. Lets write the script now to display recent post titles. Retrieving Recent Posts TitlesFollowing is the complete JavaScript code that will extract recent Post titles from the JSON feed and print the list:
As you know we have divided the above script in three parts and I will discuss it accordingly 1. Writing the Callback Function1 Here we first declared some default values to variables. In order
Note that the user can customize these options by re-setting them during Invoking. If incase the user does not mention any values the defaults will be used. 2 Next since we are printing the Title list we therefore first printed <ul class="mbtlist"> just before starting the function. 3 We then started a for Loop from 0 till the user assigned List count.
4 After declaring some variables for printing the list, Title and title URL we started our first loop to fetch the "Title URL"
5 Next we wrote the script to extract the Title String i.e. Title text. if (json.feed.entry[i].title!= null)
INFO The substr() method extracts parts of a string, beginning at the character at the specified position, and returns the specified number of characters.6 Later we printed the list inside <li> tags using the variable listing 7 Finally we closed the unordered list outside the first loop using
Note that the entire data is extracted and printed inside <li> tags. The loop which is fetching and printing the list is this:
This loop itself is enclosed inside the <ul> and </ul> tag. Thus the Recent Posts are displayed! :) 2. Invoking the callback FunctionYou might have noticed that this time the invoking section of the code looks a little unfamiliar. Previously:
Now:
We simply printed the Inline JavaScript code inside a document.write() function just to give our widget a more dynamic control. This way we can assign variables inside the Invoking section and give more control to the user. This makes the code easy for the user who can then easily assign custom values for ListBlogLink , ListCount and TitleCount . 3. Defining CSS StylesNo one would like a dull and grey list unless you add some custom colors to it. Without the style sheet the widget will look like this: I am sure no one would like it without colors therefore in the last part we added some CSS touch to the list! :) Have Questions?I tried my best to make it as simple as possible, if incase you could not understand any step, feel free to ask your question by posting your comment below. In our next tutorial, we will explain how to display recent posts by specifying a Label. Recent Titles will display based on the category specified by the user. Happy weekend buddies! => |
You are subscribed to email updates from My Blogger Tricks. To stop receiving these emails, you may unsubscribe now. | Email delivery powered by Google |
Google Inc., 1600 Amphitheatre Parkway, Mountain View, CA 94043, United States |
0 comments:
Post a Comment