Adding tags to sNews CMS
Updated February 3rd, 2009, please read this entry for the updated tutorial.
OK, here's a neat one for those sNews CMS users out that like the "tags" functionality of other CMS & Blogs. It helps if you're unafraid to hack around in your snews.php file a bit.
The idea here is fairly simple, we will take the existing keywords (the "Keywords META Tag" option under customize) and extract them and format them so they appear as a list of clickable links. You will want to make sure your keywords are properly formatted, enter them with a comma and single whitespace separating them, like so;
keyword1, keyword2, keyword3
The clickable links must be used to pass the keyword to the existing search function. The only problem with that is the sNews search form currently uses the POST method; I would change this to GET regardless, so users can save their searches! So let's change the search form, as always BACK UP your snews.php file first, then find the function searchform.
// change the "method" from "post" to "get"
form id="search_engine" method="get"
OK, now find the function search, and replace POST with GET.
// change the "method" from "post" to "get"
$search_query = clean(cleanXSS($_GET['search_query']));
and then in the function center
// change the "method" from "post" to "get"
switch(true) {
case isset($_GET['search_query']):
search(); return; break;
case isset($_POST['comment']):
comment('comment_posted'); return; break;
OK, so the search should work fine again, now it's using the GET method. Next we need to look at pulling the tags out from the articles. To start we have to actually obtain the tags from the database. Find the function articles. Look for
if ($_ID || (!$_catID && $frontpage != 0)) {
and update the $query_articles variable by adding the highlighted code
if ($_ID || (!$_catID && $frontpage != 0)) {
if (!$_ID) $_ID = $frontpage;
// article or page, id as indentifier
$query_articles = 'SELECT
a.id AS aid,title,a.seftitle AS asef,a.category AS acat,a.keywords_meta AS keywords,text,a.date,
a.displaytitle,a.displayinfo,a.commentable,a.visible
FROM '._PRE.'articles'.' AS a
WHERE id ='.$_ID.$visible;
} else {
Now look for
// get the rows for category
if ($_catID) {
and update the $query_articles variable by adding the highlighted code
// get the rows for category
if ($_catID) {
$query_articles = 'SELECT
a.id AS aid,title,a.seftitle AS asef,a.keywords_meta AS keywords,text,a.date,
a.displaytitle,a.displayinfo,a.commentable,a.visible
FROM '._PRE.'articles'.' AS a
WHERE position = 1
AND a.published =1
AND category = '.$_catID.$visible.'
ORDER BY artorder ASC,date DESC
LIMIT '.($currentPage - 1) * $article_limit.','.$article_limit;
} else {
$query_articles = 'SELECT
a.id AS aid,title,a.seftitle AS asef,a.keywords_meta AS keywords,text,a.date,
displaytitle,displayinfo,commentable,a.visible,
c.name AS name,c.seftitle AS csef,
x.name AS xname,x.seftitle AS xsef
FROM '._PRE.'articles'.' AS a
LEFT OUTER JOIN '._PRE.'categories'.' as c
ON category = c.id
LEFT OUTER JOIN '._PRE.'categories'.' as x
ON c.subcat = x.id AND x.published =\'YES\'
WHERE show_on_home = \'YES\'
AND position = 1
AND a.published =1
AND c.published =\'YES\''.$visible.'
ORDER BY date DESC
LIMIT '.($currentPage - 1) * $article_limit.','.$article_limit;
}
That will slurp the keywords into the $r['keywords'] variable. We will now prepare to break the keywords apart and insert them into the article, underneath the "infoline" and admin edit links. Still within the articles function, find the 2 instances of the admin links;
} else if (_ADMIN) {
echo '<p>'.$edit_link.'</p>';
}
then insert the following code below both of the admin edit links, like this;
// insert new code here
if (trim($r['keywords']) != "") { // not empty
$article_tags = explode(', ', $r['keywords']);
$keyCount = count($article_tags);
if ($keyCount > 0) {
echo '<div class="tags">';
for ($i = 0; $i < $keyCount; $i++) {
if ($i == 0) {
$pre = "";
} else {
$pre = ", ";
}
echo $pre.'<a href="'._SITE.'?search_query='. urlencode($article_tags[$i]).'" title="See more articles on '.$article_tags[$i].'">'.$article_tags[$i].'</a>';
}
echo "</div>";
}
}
Now you need an icon, like this one
, save that and drop it into your images folder and then add the following to your style sheet. These are my colors, feel free to create your own.
.tags {
background: #fff url('../images/tags.png') left center no-repeat;
font-size: 0.9em;
margin-bottom: 14px;
padding: 0 0 2px 14px;
}
.tags a {
color: #f66363;
text-decoration: none;
}
.tags a:hover {
color: #000;
text-decoration: none;
}
You can change things around to display them only on certain articles like I have done, for example I only show the tags on true "articles", not on "pages". To show only on articles with "infoline" shown and not "pages" like mine, I wrapped the second instance of the tag code in the following;
if ($infoline == true && $_catID != 0) {
//tag code goes here
}
Also, if you use 3 letter keywords like I do (css, cms, etc), you'll want to change the default minimum character count in the search function
// change the 4 to a 3 (or whatever minimum you choose, 3 is nice though)
if (strlen($search_query) < 3 || $search_query == l('search_keywords')) {
Then change the charerror message in your language file accordingly for good measure. That should do it, unless I missed a step. In that case, use your back up snews.php (you did back it up, right?) and let me know what kind of error you're getting and I'll do my best to assist. For an example of the tags, just look down :).


Comments
RSS Comments Feed
Piotr
replace with:
After this the search should work fine.
Matt
jlhaslip
Quite impressed. karma++.
Matt