Blogger Label List for FTP Published Blogs


I use Blogger for this blog, mainly because it is easy to use. One of the features of the new Blogger that I like is the ability to add labels to posts, but because I publish via FTP there is no way to use the labels widget that is available to users of the new Blogger that publish to Blogspot blogs. Well I really wanted to be able to list all the labels I use so there is easy access to posts by categories so I came up with a quick little PHP script to do just that.

First you have to know that when the new Blogger publishes the labels to a server via FTP it creates a folder called labels and publishes all the label pages to that folder. It also makes the file names the same as the label. This makes it easy to create a script to read all the file names in the labels folder, and output the names.

Here is a copy of the PHP script that you can use to read all of the .html files in the labels folder and output them as an unordered list. Save it and place it in the labels folder of your web server. In the script it does not include the php script in the list, if you save the script with a different name than “labelslist.php” then make sure you change filename in the script.

Update: Download the code for the labels list instead of copying and pasting. Download

<?php $labelsdir=$_SERVER["DOCUMENT_ROOT"].'/labels'; $files=array(); if ($handle = opendir($labelsdir)) { $fkey=0; while (false !== ($file = readdir($handle))) { if ($file != "." && $file != ".." && $file != "index.html" && $file != "labelslist.php" && ereg(".html$",$file)) { $files[$fkey]['name'] = $file; $fkey++; } } closedir($handle); } sort($files); echo '<ul>'; foreach ($files as $key=>$value) { echo '<li><a href="/labels/'.$files[$key]['name'].'">'.ucwords(substr($files[$key]['name'], 0, -5)).'</a></li> '; } echo '</ul>'; ?>

Now if you have setup your server to process .html files as php then your server will process PHP includes that you have in your Blogger template file. Find the location in your Blogger template that you want to include the labels list and do a simple PHP include

<?php include_once($_SERVER['DOCUMENT_ROOT']."/labels/labelslist.php"); ?>

and republish. Your label list should now appear on your FTP published Blogger blog.

The script outputs an unordered list so you might want to style the list so it is similar to the rest of your blog. It is also possible to make this list look more like a tag cloud. If people want to know how to do that leave a comment and I will add another post to help create a tag cloud style list. Happy blogging!

EDIT: Just noticed that when Blogger creates the label pages that have spaces in the names it is properly encoding them with a %20 as the space. To stop the %20 from showing up and actually have a space modify the line in the code from this:

echo '<li><a href="/labels/'.$files[$key]['name'].'">'.ucwords(substr($files[$key]['name'], 0, -5)).'</a></li> ';

to

echo '<li><a href="/labels/'.$files[$key]['name'].'">'.ucwords(substr(urldecode($files[$key]['name']), 0, -5)).'</a></li> '; That should decode the %20 so a space actually shows up on the link.

EDIT: The change Blogger did has turned into more of a pain in the neck than it should have been. Check out this post titled ”Spaces in Blogger Labels - A Change” about why. Anyways, probably the best solution is to not use spaces in your labels but use a dash instead. You can then change the line of code so it looks like this:

echo '<li><a href="/labels/'.$files[$key]['name'].'">'.ucwords(substr(str_replace('-',' ',$files[$key]['name']), 0, -5)).'</a></li> ';

You would think that Blogger would know better, why didn’t they just use dashes in the label file names to begin with, that is what they do with the blog post filenames.

Categories: web-programming 
Comments