Account Options

  1. Sign in
The old Google Groups will be going away soon.
Switch to the new Google Groups.
Google Groups Home
« Groups Home
Discussions > How Do I? > SOLUTION: Labels in the sidebar automaticall when publishing via FTP
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  9 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
nebulous  
View profile  
 More options Feb 17 2007, 6:44 pm
From: nebulous
Date: Sat, 17 Feb 2007 23:44:14 -0000
Local: Sat, Feb 17 2007 6:44 pm
Subject: SOLUTION: Labels in the sidebar automaticall when publishing via FTP
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.

JB Bryant


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
boringindierockboy  
View profile  
 More options Feb 26 2007, 1:20 pm
From: boringindierockboy
Date: Mon, 26 Feb 2007 18:20:39 -0000
Local: Mon, Feb 26 2007 1:20 pm
Subject: Re: SOLUTION: Labels in the sidebar automaticall when publishing via FTP
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:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
nebulous  
View profile  
 More options Mar 4 2007, 3:30 pm
From: nebulous
Date: Sun, 04 Mar 2007 12:30:01 -0800
Local: Sun, Mar 4 2007 3:30 pm
Subject: Re: SOLUTION: Labels in the sidebar automaticall when publishing via FTP
Boringindierockboy:

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.

jb

On Feb 26, 1:20 pm, boringindierockboy wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Antti Mäki  
View profile  
 More options Mar 29 2007, 3:09 pm
From: Antti Mäki
Date: Thu, 29 Mar 2007 12:09:39 -0700
Local: Thurs, Mar 29 2007 3:09 pm
Subject: Re: SOLUTION: Labels in the sidebar automaticall when publishing via FTP
On 4 maalis, 23:30, nebulous wrote:

> Boringindierockboy:

> 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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
nebulous  
View profile  
 More options Mar 31 2007, 12:23 pm
From: nebulous
Date: Sat, 31 Mar 2007 09:23:08 -0700
Local: Sat, Mar 31 2007 12:23 pm
Subject: Re: SOLUTION: Labels in the sidebar automaticall when publishing via FTP
Antti:

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";
    }
?>
~~~~~~~~~~~~~~~~~~~~~~

Thank you!

jb

On Mar 29, 3:09 pm, Antti Mäki wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Antti Mäki  
View profile  
 More options Mar 31 2007, 3:17 pm
From: Antti Mäki
Date: Sat, 31 Mar 2007 12:17:15 -0700
Local: Sat, Mar 31 2007 3:17 pm
Subject: Re: SOLUTION: Labels in the sidebar automaticall when publishing via FTP
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/";
    }

    $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);
    echo "  <ul>\n";
    foreach ($directorylist as $filename) {
        $temp = explode(".",$filename);
        if ((count($temp)) > 1) {
            unset($temp[count($temp)-1]);}
        $displayname = implode($temp);
        echo "    <li><a href='". $startdir . $filename . "'>" .
$displayname . "</a></li>\n";
    }
    echo "  </ul>";
?>

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.

Let me know if this works.

-- Antti


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Antti Mäki  
View profile  
 More options Mar 31 2007, 4:37 pm
From: Antti Mäki
Date: Sat, 31 Mar 2007 13:37:23 -0700
Local: Sat, Mar 31 2007 4:37 pm
Subject: Re: SOLUTION: Labels in the sidebar automaticall when publishing via FTP
Actually, (while brushing my teeth), I came up with a better idea,
which will do away with relative paths altogether:

<?php
    $labelsdir = "/home/passivis/public_html/blog/labels/";
    $ignoredDirectory[] = ".";
    $ignoredDirectory[] = "..";

    if (is_dir($labelsdir)) {
        if ( $dh = opendir( $labelsdir ))
        {
            while ( ($file = readdir($dh)) != false )
            {
                if (!(array_search($file,$ignoredDirectory) > -1))
                {
                       $directorylist[]= $file;
    }}}}

    closedir($dh);
    natcasesort($directorylist);
    echo "  <ul>\n";
    foreach ($directorylist as $filename) {
        $temp = explode(".",$filename);
        if ((count($temp)) > 1) {
            unset($temp[count($temp)-1]);}
        $displayname = implode($temp);
        echo "    <li><a href='http://www.mydomain.com/blog/labels/" .
$filename . "'>" . $displayname . "</a></li>\n";
    }
    echo "  </ul>";
?>

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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
nebulous  
View profile  
 More options Apr 1 2007, 3:28 pm
From: nebulous
Date: Sun, 01 Apr 2007 19:28:19 -0000
Local: Sun, Apr 1 2007 3:28 pm
Subject: Re: SOLUTION: Labels in the sidebar automaticall when publishing via FTP
Antti:

Thank you for brushing your teeth :-) -- you solved it! Thank you
again. I hope this is beneficial to others as well.

On Mar 31, 4:37 pm, Antti Mäki wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Antti Mäki  
View profile  
 More options Apr 3 2007, 5:36 pm
From: Antti Mäki
Date: Tue, 03 Apr 2007 14:36:55 -0700
Local: Tues, Apr 3 2007 5:36 pm
Subject: Re: SOLUTION: Labels in the sidebar automaticall when publishing via FTP
If your server runs PHP5, you can use yet another, shorter version of
the code, which can be found at http://www.passivist.net/blog/2007/04/how-to-list-labels-in-sidebar-w...
.

-- Antti Mäki


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »