Many people (including myself) have been trying hard to find a way to automatically create a list of labels in a sidebar when publishing their blogs by FTP (i.e., hosting the blogs on their own server rather than on BlogSpot). The issue has been that we're limited to Classic templates, and classic templates don't offer this feature.
Some solutions have been offered, but all of them require manually keeping up a list of new labels as they are added.
With the help of some friends, I have solved the problem. You can see it in action in Keeping Abreast, my Breast Cancer news blog, at http://www.jbryant.org/bc/blog_bcnews.
The solution requires three common conditions: (1) That your website is hosted on an Apache server (though someone may figure out how to apply this to a different type of server)
(2) That you can -- and know how to -- modify your .htaccess file on your server
(3) That your server supports PHP
Assuming all of those are true, it is very simple and will take you 15 minutes tops to follow my instructions. First, though, let me explain what is happening here:
When you add labels to your posts, Blogger creates a directory right off of the blogs main directory called "labels." The only files in that directory are files named "<something>.html." Each of those html files corresponds to -- and is named after -- one of your labels. The files are collections of all posts that contain that label.
The code below adds a PHP script to your blog that reads that directory each time the blog is accessed, inserts the first part of each file's name (before the .html extension), and creates hyperlinks to the file.
Simple, eh? Ingenious, eh? Well, at least it's simple. :-)
Follow these instructions carefully:
(1) MODIFY YOUR .HTACCESS ON YOUR SERVER: Access (or create) your .htaccess file, either one that affects your whole site, or one that resides just in the directory where your blog is published. Add the following line anywhere in that file and save it:
AddHandler application/x-httpd-php .htm
What this does is allow your server to utilize PHP code that is buried inside and HTML file. It's harmless to everything else.
(2) MODIFY YOUR BLOGGER TEMPLATE I'm assuming you know how to tweak your template. If not, search these forums for instructions. I won't belabor it here. All you need to do is add the following code to your sidebar where you want it to appear (I set off the code between rows of ~~~~~~~~~ to make it clear; don't include that in your code):
~~~~~~~~~~~~~~~~~ <!-- Begin PHP script to display labels -->
<h2 class="sidebar-title">Categories</h2>Click any label below to view posts related to that topic<br>
<?php $startdir = "./labels/"; $ignoredDirectory[] = "."; $ignoredDirectory[] = ".."; //list any other files you want to ignore if (is_dir($startdir)) { if ($dh = opendir($startdir)) { while (($file = readdir($dh)) !== false) { if (!(array_search($file,$ignoredDirectory) > -1)) { $directorylist[]= $file; }}}} closedir($dh); natcasesort($directorylist); foreach ($directorylist as $filename) { $temp = explode(".",$filename); if ((count($temp)) > 1) { unset($temp[count($temp)-1]);} $displayname = implode($temp); echo "<a href='". $startdir . $filename . "'>" . $displayname . "</a><br>\n"; } ?> <!-- End PHP script to display labels --> ~~~~~~~~~~~~~~~~~
Save the Template changes and republish your blog. That's it!
I hope this helps. I will add that I have several blogs. I tried this on one of them and it produced a PHP error. I haven't yet figured out why. I have to wonder if it is somehow related to the fact that the blog is password protected (also via a .htaccess file) or maybe something unique in one of the posts. But I have successfully done this on two of my blogs (the only other ones I've used labels with) and it works beautifully. The other one is at http://www.jbryant.org/quotes/christian
If you have any problems with this, post a reply and I'll try to find time to help, but no promises. My knowledge and my time are both rather scarce.
I don't have access to my .htaccess file, but everything else is in place...what can I do? I have a set number of catagories so something manual might work...
> Many people (including myself) have been trying hard to find a way to > automatically create a list of labels in a sidebar when publishing > their blogs by FTP (i.e., hosting the blogs on their own server rather > than on BlogSpot). The issue has been that we're limited to Classic > templates, and classic templates don't offer this feature.
> Some solutions have been offered, but all of them require manually > keeping up a list of new labels as they are added.
> With the help of some friends, I have solved the problem. You can see > it in action in Keeping Abreast, my Breast Cancer news blog, athttp://www.jbryant.org/bc/blog_bcnews.
> The solution requires three common conditions: > (1) That your website is hosted on an Apache server (though someone > may figure out how to apply this to a different type of server)
> (2) That you can -- and know how to -- modify your .htaccess file on > your server
> (3) That your server supports PHP
> Assuming all of those are true, it is very simple and will take you 15 > minutes tops to follow my instructions. First, though, let me explain > what is happening here:
> When you add labels to your posts, Blogger creates a directory right > off of the blogs main directory called "labels." The only files in > that directory are files named "<something>.html." Each of those html > files corresponds to -- and is named after -- one of your labels. The > files are collections of all posts that contain that label.
> The code below adds a PHP script to your blog that reads that > directory each time the blog is accessed, inserts the first part of > each file's name (before the .html extension), and creates hyperlinks > to the file.
> Simple, eh? Ingenious, eh? Well, at least it's simple. :-)
> Follow these instructions carefully:
> (1) MODIFY YOUR .HTACCESS ON YOUR SERVER: > Access (or create) your .htaccess file, either one that affects your > whole site, or one that resides just in the directory where your blog > is published. Add the following line anywhere in that file and save > it:
> AddHandler application/x-httpd-php .htm
> What this does is allow your server to utilize PHP code that is buried > inside and HTML file. It's harmless to everything else.
> (2) MODIFY YOUR BLOGGER TEMPLATE > I'm assuming you know how to tweak your template. If not, search these > forums for instructions. I won't belabor it here. All you need to do > is add the following code to your sidebar where you want it to appear > (I set off the code between rows of ~~~~~~~~~ to make it clear; don't > include that in your code):
> ~~~~~~~~~~~~~~~~~ > <!-- Begin PHP script to display labels -->
> <h2 class="sidebar-title">Categories</h2>Click any label below to > view posts related to that topic<br>
> <?php > $startdir = "./labels/"; > $ignoredDirectory[] = "."; > $ignoredDirectory[] = ".."; //list any other files you want to > ignore > if (is_dir($startdir)) { > if ($dh = opendir($startdir)) { > while (($file = readdir($dh)) !== false) { > if (!(array_search($file,$ignoredDirectory) > -1)) { > $directorylist[]= $file; > }}}} > closedir($dh); > natcasesort($directorylist); > foreach ($directorylist as $filename) { > $temp = explode(".",$filename); > if ((count($temp)) > 1) { > unset($temp[count($temp)-1]);} > $displayname = implode($temp); > echo "<a href='". $startdir . $filename . "'>" . > $displayname . "</a><br>\n"; > } > ?> > <!-- End PHP script to display labels --> > ~~~~~~~~~~~~~~~~~
> Save the Template changes and republish your blog. That's it!
> I hope this helps. I will add that I have several blogs. I tried this > on one of them and it produced a PHP error. I haven't yet figured out > why. I have to wonder if it is somehow related to the fact that the > blog is password protected (also via a .htaccess file) or maybe > something unique in one of the posts. But I have successfully done > this on two of my blogs (the only other ones I've used labels with) > and it works beautifully. The other one is athttp://www.jbryant.org/quotes/christian
> If you have any problems with this, post a reply and I'll try to find > time to help, but no promises. My knowledge and my time are both > rather scarce.
I apologize for the slow reply. You will definitely have to be able to modify your .htaccess for this. If you do have PHP but don't have access to your .htaccess, you should contact your Web host or ISP and ask them to modify it for you. they should have no issue with doing so.
Incidentally, my solution didn't trun out to be as perfect as I'd hoped. It works great for the main blog page. But Blogger uses the same template to display archived posts. Since archived posts are stored in a different directory, there is no "./labels" directory that the script can access. This results in errors displayed on archived pages where you wuld expect to see the labels.
I'm working on a solution to this, If anyone posts a reply to this message letting me know they are interested, I'll post the solution when I come up with it.
> I don't have access to my .htaccess file, but everything else is in > place...what can I do? I have a set number of catagories so something > manual might work...
> On Feb 17, 3:44 pm, nebulous wrote:
> > Many people (including myself) have been trying hard to find a way to > > automatically create a list of labels in a sidebar when publishing > > their blogs by FTP (i.e., hosting the blogs on their own server rather > > than on BlogSpot). The issue has been that we're limited to Classic > > templates, and classic templates don't offer this feature.
> > Some solutions have been offered, but all of them require manually > > keeping up a list of new labels as they are added.
> > With the help of some friends, I have solved the problem. You can see > > it in action in Keeping Abreast, my Breast Cancer news blog, athttp://www.jbryant.org/bc/blog_bcnews.
> > The solution requires three common conditions: > > (1) That your website is hosted on an Apache server (though someone > > may figure out how to apply this to a different type of server)
> > (2) That you can -- and know how to -- modify your .htaccess file on > > your server
> > (3) That your server supports PHP
> > Assuming all of those are true, it is very simple and will take you 15 > > minutes tops to follow my instructions. First, though, let me explain > > what is happening here:
> > When you add labels to your posts, Blogger creates a directory right > > off of the blogs main directory called "labels." The only files in > > that directory are files named "<something>.html." Each of those html > > files corresponds to -- and is named after -- one of your labels. The > > files are collections of all posts that contain that label.
> > The code below adds a PHP script to your blog that reads that > > directory each time the blog is accessed, inserts the first part of > > each file's name (before the .html extension), and creates hyperlinks > > to the file.
> > Simple, eh? Ingenious, eh? Well, at least it's simple. :-)
> > Follow these instructions carefully:
> > (1) MODIFY YOUR .HTACCESS ON YOUR SERVER: > > Access (or create) your .htaccess file, either one that affects your > > whole site, or one that resides just in the directory where your blog > > is published. Add the following line anywhere in that file and save > > it:
> > AddHandler application/x-httpd-php .htm
> > What this does is allow your server to utilize PHP code that is buried > > inside and HTML file. It's harmless to everything else.
> > (2) MODIFY YOUR BLOGGER TEMPLATE > > I'm assuming you know how to tweak your template. If not, search these > > forums for instructions. I won't belabor it here. All you need to do > > is add the following code to your sidebar where you want it to appear > > (I set off the code between rows of ~~~~~~~~~ to make it clear; don't > > include that in your code):
> > ~~~~~~~~~~~~~~~~~ > > <!-- Begin PHP script to display labels -->
> > <h2 class="sidebar-title">Categories</h2>Click any label below to > > view posts related to that topic<br>
> > Save the Template changes and republish your blog. That's it!
> > I hope this helps. I will add that I have several blogs. I tried this > > on one of them and it produced a PHP error. I haven't yet figured out > > why. I have to wonder if it is somehow related to the fact that the > > blog is password protected (also via a .htaccess file) or maybe > > something unique in one of the posts. But I have successfully done > > this on two of my blogs (the only other ones I've used labels with) > > and it works beautifully. The other one is athttp://www.jbryant.org/quotes/christian
> > If you have any problems with this, post a reply and I'll try to find > > time to help, but no promises. My knowledge and my time are both > > rather scarce.
> Incidentally, my solution didn't trun out to be as perfect as I'd > hoped. It works great for the main blog page. But Blogger uses the > same template to display archived posts. Since archived posts are > stored in a different directory, there is no "./labels" directory that > the script can access. This results in errors displayed on archived > pages where you wuld expect to see thelabels.
> I'm working on a solution to this, If anyone posts a reply to this > message letting me know they are interested, I'll post the solution > when I come up with it.
> jb
I found this post while googling for a way to get the label list on the sidebar. I solved the directory problem by replacing:
$startdir = "./labels/";
with:
$currentdir = getcwd(); switch ($currentdir) { // The current directory is the main directory of your blog. case "/home/myaccount/public_html/myblogdirectory": $startdir = "./labels/"; break; // Current directory is the labels directory. case "/home/myaccount/public_html/myblogdirectory/labels": $startdir = "../labels/"; break; // Basically the only options left are the archive directories. default: $startdir = "../../labels/"; }
This works on blog. I'm using the "monthly" archives. I'm not sure what kind of directory structure the other archiving methods use, so I can't guarantee that the code will work with them. Has JB or someone else come up with better, more robust solutions?
-- Antti Mäki
PS. Thanks for sharing the original code snippet, JB.
Thank you for this. However, it is not working for me. I suspect the problem is my like of PHP knowledge.
I get three errors in the sidebar on my main page rather than a list of labels. The three errors I get (without the line numbers because that would be irrelevant to you) are:
* Warning: closedir(): supplied argument is not a valid Directory resource * Warning: natcasesort() [function.natcasesort]: The argument should be an array * Warning: Invalid argument supplied for foreach()
Below is the complete script as I modified it with your addition. Can you tell me what I've done wrong?
~~~~~~~~~~~~~~~~~~~~~~ <?php $currentdir = getcwd(); switch ($currentdir) { // The current directory is the main directory of your blog. case "/home/myaccount/public_html/myblogdirectory": $startdir = "./labels/"; break; // Current directory is the labels directory. case "/home/myaccount/public_html/myblogdirectory/labels": $startdir = "../labels/"; break; // Basically the only options left are the archive directories. default: $startdir = "../../labels/"; } $ignoredDirectory[] = "."; $ignoredDirectory[] = ".."; if (is_dir($startdir)) { if ($dh = opendir($startdir)) { while (($file = readdir($dh)) !== false) { if (!(array_search($file,$ignoredDirectory) > -1)) { $directorylist[]= $file; }}}} closedir($dh); natcasesort($directorylist); foreach ($directorylist as $filename) { $temp = explode(".",$filename); if ((count($temp)) > 1) { unset($temp[count($temp)-1]);} $displayname = implode($temp); echo "<a href='". $startdir . $filename . "'>" . $displayname . "</a><br>\n"; } ?> ~~~~~~~~~~~~~~~~~~~~~~
> > Incidentally, my solution didn't trun out to be as perfect as I'd > > hoped. It works great for the main blog page. But Blogger uses the > > same template to display archived posts. Since archived posts are > > stored in a different directory, there is no "./labels" directory that > > the script can access. This results in errors displayed on archived > > pages where you wuld expect to see thelabels.
> > I'm working on a solution to this, If anyone posts a reply to this > > message letting me know they are interested, I'll post the solution > > when I come up with it.
> > jb
> I found this post while googling for a way to get the label list on > the sidebar.I solved the directory problemby replacing:
> $startdir = "./labels/";
> with:
> $currentdir = getcwd(); > switch ($currentdir) { > // The current directory is the main directory of your blog. > case "/home/myaccount/public_html/myblogdirectory": > $startdir = "./labels/"; > break; > // Current directory is the labels directory. > case "/home/myaccount/public_html/myblogdirectory/labels": > $startdir = "../labels/"; > break; > // Basically the only options left are the archive > directories. > default: > $startdir = "../../labels/"; > }
> This works on blog. I'm using the "monthly" archives. I'm not sure > what kind of directory structure the other archiving methods use, so I > can't guarantee that the code will work with them. Has JB or someone > else come up with better, more robust solutions?
> -- Antti Mäki
> PS. Thanks for sharing the original code snippet, JB.
I got the same three error messages when I had set up incorrect directory paths in the code. I forgot to mention, that you should change the paths to the correct "cases" in the switch structure. So for example, my switch looks like this:
switch ($currentdir) { case "/home/passivis/public_html/blog": $startdir = "./labels/"; break; case "/home/passivis/public_html/blog/labels": $startdir = "./"; break; default: $startdir = "../../labels/"; }
My blog is located in the blog-directory under the public_html- directory. So basically the first case is tells the code snippet, that if the web page is located in that directory (e.g. www.domain.com/blog/index.html), the labels can be found in labels-directory under the blog directory (so the $startdir is set to ./labels/). Respectively the second case says, that if the web page is under labels-directory (eg. www.domain.com/blog/labels/general.html), the labels are in that directory, too. Lastly the default case is applied to archived pages (e.g. www.domain.com/blog/2007/03/entry-name.html), and in their cases the labels-directory can be found two directories down the directory hierarchy.
If you have trouble finding what are the absolute paths for your blog directory, you can print the value that the getcwd()-command (GET Current Working Directory) returns by simply echoing it. This way the path of the current directory is printed, for example, before the three error messages. For simplicitys sake, the whole code snippet that works on my blog is:
<?php $currentdir = getcwd(); // for debugging: echo ($currentdir);
switch ($currentdir) { case "/home/passivis/public_html/blog": $startdir = "./labels/"; break; case "/home/passivis/public_html/blog/labels": $startdir = "./"; break; default: $startdir = "../../labels/"; }
As you can see, I also put the label-links inside a list, so that I can more easily change the their styles to match the styles of the "previos posts" and "archive"-links.
On the first row, $labelsdir contains the absolute path to your labels directory. Once again you can use the getcwd() on any page in the labels directory to find out what this directory is. Note that you have to add the lash to the end of the string that the getcwd() will return. Also note that on the line where the links are echoed, you have to write the location of your labels directory manually. So, where the original code was intended to work directly on any site, this version requires a little bit of manual fine tuning in order to work.
> On the first row, $labelsdir contains the absolute path to your labels > directory. Once again you can use the getcwd() on any page in the > labels directory to find out what this directory is. Note that you > have to add the lash to the end of the string that the getcwd() will > return. Also note that on the line where the links are echoed, you > have to write the location of your labels directory manually. So, > where the original code was intended to work directly on any site, > this version requires a little bit of manual fine tuning in order to > work.