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.
Comments
Jay Harper
Thanks!!!<br/><br/>I've been looking for something like this for a while and couldn't find it. I'm still convinced there must be a blogger template tag to do the trick otherwise why would nearly all the files in the blog be republished when the post has labels whereas just a few files are republished when the post doesn't have labels?<br/><br/>Anyway, thanks again... It meets my needs exactly...
LGR
Jay,<br/>There should be a template tag to do this for those that FTP publish. If you ever find it please drop me a note. In the meantime, this does work and I am happy that you find it useful.
Label Lists for Blogger Templates &raquo; Slicksurface - Tech, Design &amp; SEO Blog
[...] at long last I did find a post by Lee Robertson that explains what to do, but you&#8217;ll need to have PHP installed on your server and be able to make PHP execute in [...]
Rob
Any solution on how to display the current label as the page's h1 line if a page from the labels folder is displayed? thanks and best Rob
LGR
I have an idea of how it could be done using some PHP on a FTP published blog. I will give it a try tonight and make a post about it if I can get it to work.
bilecem
Greate tip. i will apply it as soon as posible.Thank you for the post :)
LGR
Thanks. It is still working on my FTP published Blogger blog.
Ethan
I attempted your solution but received the following errors. Warning: Division by zero in /usr/www/users/scrippy/palimpsest/index.html on line 529 Warning: Division by zero in /usr/www/users/scrippy/palimpsest/index.html on line 529 Warning: include_once(/usr/www/users/scrippyphp”) [function.include-once]: failed to open stream: No such file or directory in /usr/www/users/scrippy/palimpsest/index.html on line 529 Warning: include_once() [function.include]: Failed opening '/usr/www/users/scrippyphp”' for inclusion (include_path='.:/usr/local/lib/php') in /usr/www/users/scrippy/palimpsest/index.html on line 529 I'm not a very experienced PHP user. Any hints on what I'm doing wrong?
LGR
What is on line 529 where is is dividing by zero? Send me an message using the contact form with the code and the URL so I can take a look.
Ethan
Thank you very much. It's working now. I put together a few other code examples and managed to build a dynamically sizing tag cloud.
LGR
Great! I used it as a tag cloud for a while as well at one time. I zipped up the file and made it available for people to download if they want instead of copying and pasting. Perhaps that will stop what caused your problem in the future. I also pasted the code in again so it is a little cleaner.
Andrew Wilcox
Hi I get the same problem Ethan. It is the line which calls the php file. What is the solutions?
LGR
All I did with the code for Ethan was clean it up. Try downloading the file of the code instead of copying and pasting it out of the page. <a href="http://lgr.ca/blog/wp-content/uploads/2007/02/labelslist.zip" rel="nofollow">Download</a>