Tags list, an add-on to my tags mod

Tags list, an add-on to my tags mod

I just whipped this one up per a request from Brian over at The Miniblog (check it out, excellent design, shows what sNews can look like with the right talent). If you have installed my tags mod for sNews, then you can use this to retrieve a list of your tags, comma separated, and clickable (to pass the tag to the search engine).

This mod does not rank your tags by occurrences as a standard tag cloud would, though that's possible to do. So back up your snews.php file and let's get started.

First, we need to add the function, so copy & paste the following function just above the final php closing tag (the ?> at the very bottom) in snews.php;

If you're using my tags mod version 1 and want to continue using that, simply replace the highlighted text with ?search_query= instead of tag/

// this function grabs all the meta_keywords, removes duplicates and builds a comma separated list
function get_taglist($showpages="0") {
    if ($showpages == "1") {
        $pages_query = "category != '-1' ";
    } else {
        $pages_query = "category > 0 ";
    }
    $now = date("Y-m-d H:i:s",time());
    $sql = "SELECT distinct(keywords_meta) FROM "._PRE."articles WHERE ".$pages_query." AND date <= '".$now."' AND published='1' AND visible = 'YES' AND keywords_meta != ''"; 
    $result = mysql_query($sql);
    $count = mysql_num_rows($result);
    $x = "1";
    while ($row = mysql_fetch_array($result)) {
        if ($x < $count) {
            $suffix = ", ";
        } else {
            $suffix = "";
        }
        $tagslist .= $row['keywords_meta'].$suffix; 
    $x++;
    }

    $tagslist = explode(", ",$tagslist); 
    $tagslist = array_unique($tagslist);
    sort($tagslist);
    $keyCount = count($tagslist);
    if ($keyCount > 0) {
        echo '<div class="taglist">';
        for ($i = 0; $i < $keyCount; $i++) {
            if ($i == 0) {
                $pre = "";
            } else {
                $pre = ", ";
            }
            if (!empty($tagslist[$i])) {
                echo $pre.'<a href="'._SITE.'tag/'.urlencode($tagslist[$i]).'/" title="See more articles on '.$tagslist[$i].'">'.$tagslist[$i].'</a>';
            }
        }
        echo "</div>";
    }
}

Now, all you need to do is add the following function call to your index.php file where you want the list displayed, change the "0" to a "1" to show tags from sNews pages, or leave at "0" to display tags from only articles;

<?php get_taglist("0"); ?>

That should be it, if you want to change the styling, all the links are inside a div with the class of "taglist", so add that to your stylesheet and have fun. Below is an example of the results on my site... slightly different format as I'm using SEF links for my tags now, I'll be releasing that mod later on... peace.

[func]get_taglist:|:0[/func]

Update #1: Jan 23rd, 2009 @ 8:35 pm - corrected div class style instructions, it's class is taglist, not tagslist. Also updated the query to not show the keywords if the article is published in the future.

Update #2: Jan 26th, 2009 @ 5:09 pm - added the sort() function to format the tags list alphabetically.

Update #3: Mar 3rd, 2009 @ 6:16 am - added trailing to tag links.

Tags

 

You might like

Comments


Hey Matt, liking this one a lot but I think I'm doing something wrong. I have the tag list implemented on inkpattern.com (if you feel inclined to take a look) but the tag links don't work as expected -- certainly not like they do here. Clicking on any tag just brings you back to the start page. I noticed just now that my tags don't have a trailing slash but yours do ... that might be it. If you have any suggestions, I'm all ears. Meanwhile I'll do some bugchecking to see what I've done wrong.

Very nice mod though. Cheers!


Hi Fred,

hmmm, looks like you're correct about missing the trailing slash, however I don't think that would actually throw any error, except maybe a duplicate content message in Google Webmaster Tools, but that's another story.

You can fix that by adding the slash like below;

echo $pre.'<a href="'._SITE.'tag/'. urlencode($tagslist[$i]).'<span class="highlight">/</span>" title="See more articles on '.$tagslist[$i].'">'.$tagslist[$i].'</a>';


Fred, do you have one of my tags mod installed? If you haven't installed one of the tags mod, you'll need to do that first, as the tag list and tag cloud mods are add-ons and require the tags mod and it's tag search function.

http://mdj.us/snews-cms/hacks-mods/tags-mo...


Hi Matt,
I have it working locally now. Not sure where I'd gone wrong because I did have your tag mod version installed, but instead of trying to find the error I just rolled back to a previous version and reinstalled the tag mods. And it's working now so... No problems!
Thanks!


Great, that was easy! :)

Yeah, "modding" isn't exactly simple for sNews, it's more like hack'n things up, lol, it can be easy to miss a step.

Therein also lies the beauty of sNews... I can bend it to my will :)

By the way, I enjoy your site, I stop by several times a week.


I wish life was easy...
Unfortunately something on my domain server screws this up. As I said before, it works locally with MAMP (PHP5/SQL5), but not on my domain server which also happens to be running PHP5 and SQL 5 (haven't checked the actual dot numbers to see if there's a difference there. It's really strange. The mod is good, my host is ... odd. :/


bah- figured it out. too stupid for words...
(forgot to upload the lang file... meh-)
sorry


LOL Fred, I do that kinda crap all the time.

You actually just gave me the idea for my next little "mod". Notice how you have to enter your Name/Website/Email with every comment, this info should be sticky if you choose.

So I'm going to mod the comments to have a "remember me" check box so it'll set a cookie and remember your reader's name and website info if they like.

Hmmm, now I just have to find the free time!


The weirdest part is that it (the tag cloud thing) works as it should on my local server --MAMP-- but not on my remote server where any tag link will throw a 404. I *think* it has to do with how my domain host uses the .htaccess but I'm not sure. it's a virtual server setup (servage.net) and they do warn about using specific .htaccess files can upset the server. Seems that's what's happening for me. Shame, the mod works really nicely otherwise.

Oh and I like this comment preview thingy. Very swish, if there is such a word. lol


Fred, that's odd, if the rest of your site uses mod_rewrite just fine. I checked out the cloud on your site, one thing you'll find is because we're using MySQL full-text, the minimum character limit on the keywords is 4, kind a of pain I know, especially for common acronyms like CSS, IE, etc.

Anyways, back on topic... if you're getting a 404 header from sNews, it's likely because it's missing the hard-coded page. Can you verify the "tag" page is in the cat_listSEF?

$l['cat_listSEF'] = 'archive,contact,sitemap,tag,login,administration,admin_category,admin_article,article_new,extra_new,page_new,snews_categories,snews_articles,extra_contents,snews_pages,snews_settings,snews_files,logout,groupings,admin_groupings';

The comment preview is just a small snippet of code if you use jQuery :)

http://mdj.us/web-development/ajax-javascr...


Uh oh... him again!

Oui. It's me: inspecteur Sven. The buggy man.
I got a curious issue with the taglist Mod.

The mod was working fine but I had to change my htaccess file recently and this tags page Mod didn't work anymore on the distant website. But locally it still works.
In my Tags page I call the function by php get_taglist("1");.
The breadcrumb says "you're on the tags page" but it shows the home page.
As I use the body id Mod I saw that the body id for the tags page was id="administration" and not id="tags".
I've checked if something was wrong in my code with this Mod. An nope.
Of course both "tags" and "tag" are in my $l['cat_listSEF'].
Any idea of the reason of this issue?
Thanks in advance Matt.
PS:I can't see the "highlighted code"???


Hi Sven,

Thanks, I've been cleaning up my stylesheet and some older "highlights" were a different style, I have fixed it now.

As for the strange error, that's very odd, what kind of header is the server sending? Are you getting a 200? What change did you make to .htaccess?


Hi Matt. The bug is online.

See http://on-air.hiseo.fr/tags/...

I got a 200 OK.

Here is the online htaccess:
------
RewriteEngine On
RewriteBase /
RewriteRule ^(.+)\)$ $1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.+)$ $1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ index.php?category=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^html5/html5-ameliorer-referencement/\)$ http://on-air.hiseo.fr/html5/html5-amelior... [R=301,L]
RewriteRule ^fatras/google-indexer-contenu/\)$ http://on-air.hiseo.fr/fatras/google-index... [R=301,L]
RewriteRule ^sitemap.xml$ http://on-air.hiseo.fr/sitemap-carnet.xml.gz [R=301,L]
RewriteRule ^html5/html5-grands-comptes-gmf/\)$ http://on-air.hiseo.fr/html5/html5-grands-... [R=301,L]
---------------
locally I have this body id="administration" too but the page is printed. Weirdo... weirdo...


Sorry... me again.
Did you change the link I gave above and add?
/.../
adding this makes the access OK.

About the WAI case: look at the answer given: there's no result for AI.
Where's is the "W" too???

I'm getting totally nuts.


The 2nd one with the 1st option.


Hmmm, I think you mean the second option, because it works for you when the case is lowered, and the first option uses full-text, which has 4 character minimum by default.

OK, so it looks like two problems in all, first the "tags" page displays the home page. Second, the tags seem to need to be lower case for REGEXP (shouldn't be the case, but whatever).

First, within the tagsearch function, add the bit below

$tags_query = trim($tags_query);
<span class="highlight">$tags_query = strtolower($tags_query); // add me</span>

Let me know if that fixes the individual tag view.


If that works, but shows the tag in lower case, then we'll want to reference the original tag query, so do something like this;



$tags_query = trim($tags_query);
<span class="highlight">$orig_query = $tags_query; // add me first!
$tags_query = strtolower($tags_query); // add me</span>
and then change the two instances where it displays the tags_query to disply the original (un-lower cased) query;
stripslashes(entity($tags_query))
to
stripslashes(entity(<span class="highlight">$orig_query</span>))


Gee! Found out that line in the function:
$tags_query = ereg_replace("\W", " ", $tags_query);
Where does it comme from? I ignore.
Commenting it out made the W appears.

Seems that there is the TAGS page issue only.
Where does this string "/.../"" comes from?


Hmmm, that line should replace non-word characters with blank space, but the ereg functions are deprecated and shouldn't be used anyways. We probably stuck that in there as a test or something?

Hmmm... just looked at your tags page again, seems to be working now?


lol, strange, I did see it before (not now again).

There was a left floated image of some sort. The tags were wrapped around it...


Yeap Matt that's it.
It's the poster of Dada.
You mean the days before this morning?


No, when you were "fixing" the tag pages, I looked at the /tags/ page and saw it.

So odd.


Lucky guy.;-)
I'm sure you saw it.
I didn't.
Same files locally.
Not the same htaccess.
/tags/ works on my machine, but must go to /tags/.../ to see it on line. Incredible.
Why do both have this body id: body id="administration"
They should have body id="tags"?

Ah! Forget to tell, I'm using tagcloud version#2.
You know: the one with "see more tags".
Now I have that message :"Tags must be at least 2 characters!"



(optional, not publicly displayed) (optional)

Copyleft 2002 - 2014 Matt Jones
Hand crafted with HTML5 & CSS3
↑ Back to top