Labels in the sidebar

closeHey, just so you know ... this post is now about 15 years and 10 months old. Please keep that in mind as it very well may contain broken links and/or outdated information.

Apparently if you host your Blogger blog on your own server (like I do) instead of Google’s blogspot.com you miss out on some cooler features of the service (like advanced Layouts). Another missing feature is the widget that inserts a list of tags/labels used in posts into the sidebar.

Thankfully, I found a blog post that detailed a way to dynamically add the list of labels using embedded PHP. It doesn’t put the total number of posts with each label at the end like the blogspot widget does, but it’s better than nothing.

5 Comments

  1. I modified the PHP code so that the totals are displayed next to each label. Here’s the final result (obviously replace the paths and URL as necessary):

    <?php
    $files = scandir("/var/www/public/blog/labels/");
    $ignored[] = ".";
    $ignored[] = "..";
    $files = array_diff($files, $ignored);
    
    echo "  <ul>n";
    foreach ($files as $filename) {
    $temp = explode(".", $filename);
    $label = $temp[0];
    $cmdline = "grep "\"post-body\"" /var/www/public/blog/labels/" . $filename . " | wc -l";
    $postcount = shell_exec($cmdline);
    echo "    <li><a href="https://www.windracer.net/blog/labels/" . $filename . "">" . $label . "</a> [" .
      trim($postcount) . "]</li>n";
    }
    echo "  </ul>";
    ?>

    Basically, the new lines search (via grep) through the current label HTML file for the “post-body” tag and count the occurences (via wc). That number is then displayed in brackets next to the label. These changes assume your web server is running Linux.

  2. If your host doesn’t allow PHP to shell_exec, or if you’re running on a platform other than Linux, replace the first two lines in green listed above with these:

    $lines = file_get_contents("/var/www/public/blog/labels/" . $filename);
    $postcount = substr_count($lines, "="post-body"");

    Thanks to Postable and The Blogger Guide for the assistance in formatting my code for a blog post.

  3. Just curious why you aren’t running something like wordpress on your linux server, rather than hosting your own blogger account? What made you go with blogger? Just curious…

  4. Hi, I’ve tried all the different codes and this one as well, which seems to be the latest. But nothing works!
    If I don’t have a .htaccess file, the code just appears as if it was some text and if I have it, the code disappear, the list doesn’t come up and everything that is coded below it and the side background disappear!
    Do you have any further help to provide?
    Thanks!

  5. @admin: it sounds like maybe your web server isn’t interpreting the embedded PHP correctly. If you’re using Apache, try adding this to your .htaccess file:

    AddType application/x-httpd-php .php .phtml .php3 .html

    That tells Apache to process .php files with the php processor (this assumes your web server is properly configured to use php in the first place).

    Also, make sure you’ve removed the line breaks in my code. The indented lines in green should be a single line.

Leave a Reply

Your email address will not be published. Required fields are marked *