Light-weight related articles mod for sNews 1.7
Here is the next evolution of my related articles/posts/entries mod for sNews CMS. It's very straight forward, all the information needed is contained in the single file, mod.related-light.php.
What exactly does this mod do?
Basically this mod will take the words within the article title as well as the meta keywords and attempt to find closely related articles to present to your visitors... which they will hopefully find useful and stay on your site. You can see it in action at the bottom of this article.
This mod is a lot lighter than my previous beta related articles mod, uses but one MySQL query, and matches title & keywords (if you toggle $match_tags setting to TRUE) against titles, keywords, descriptions, and article text. I will also give a brief install walk through here, so let's get started.
First download the mod zip file here;
VERSION history
[1.0.2] - matches IN BOOLEAN to avoid to 50% threshold
[1.0.1] - corrected category name when displayed
[1.0.0] - initial release
Extract the zip file, modify the variables in the CONFIG section shown below, then upload mod.related-light.php to the same folder as your snews.php file.
/**************************** CONFIG ****************************/ $debug = 'FALSE'; // toogle to TRUE to print debugging information via comments in the HTML source $lang_title = 'Related articles:'; // the title you would like printed out $use_same_cat = 'FALSE'; // toggle to TRUE to restrict matches to the parent, child or sibling categories $strict_cat = 'FALSE'; // toogle this to TRUE to further restrict matches to the EXACT same category, TRUE over-rides use_same_cat $display_cat = 'FALSE'; // toggle to TRUE to display the category name in parenthesis beside the matches $post_limit = '5'; // Max number of related articles to show $match_tags = 'FALSE'; // toggle to TRUE to match meta description keywords also
Now back up your snews.php and work on a copy, within the function articles, look for the following code;
} else if (empty($currentPage)) {
if ($infoline == true) {
$tag = explode(',', tags('infoline'));
foreach ($tag as $tag ) {
switch ($tag) {
case 'date':
echo $a_date_format;
break;
case 'readmore':
case 'comments': ;
break;
case 'edit':
if (_ADMIN) {
echo ' '.$edit_link;
}
break;
default:
echo $tag;
}
}
} else if (_ADMIN) {
echo '<p>'.$edit_link.'</p>';
}
}
and add the following code BELOW it
if ($infoline == true && $_catID != 0) { // displays it only on regular articles (not pages) and only when the article is viewed on it's own page
include('mod.related-light.php');
}
Now set up the full text indexes on your MySQL table if you haven't already.
ALTER TABLE articles ADD FULLTEXT(text, title, keywords_meta, description_meta);
Now add some .related_posts styles to your stylesheet, something like
div.related_posts {
margin: 5px 0;
}
ul.related_posts {
margin: 0;
padding: 0;
list-style: none;
}
ul.related_posts li{
padding-left:16px;
margin-bottom:.2em;
background-image:url('../images/related.png');
background-repeat:no-repeat;
background-position:0 .2em;
}
Here's the icon I used...
... (from pinvoke). That should be it, upload your modified stylesheet and then your modified snews.php and you should be all set.



Comments
RSS Comments Feed
JamesG.
Matt
http://www.mdj.us/extras/sidebar-related.txt
JamesG.
JamesG.
It helped me a lot, but I still have one question. What do I have to use in a function that I only want to show up on an article page in the sidebar?
It's quite easy, I only want to show a text block in the sidebar when you are at an article page.
Thanks in advance!
Kind regards
James
Matt
Something like this?
http://www.mdj.us/snews-cms/hacks-mods/php-to-display-something-in-snews-only-when-viewing-an-art...
You could also create an extra and add that into the sidebar.
JamesG.
John Flower
Fred K
The scenario is that I'm using a "feature" article block on the home page, where I print an article that has been set (in admin) as feature. I'm using a modified version of the articles with intro text mod (by Vasile Rusnac originally). It would be really cool to get related articles in there as an aside to the article summary (which is the content that is printed on the home page). I've done all the steps required by your mod and it doesn't show any related articles - there are related articles, both in terms of keywords and other content as well as the article title. I'm guessing it's not possible, at least not in the current configuration ... and when I think about it some more I guess it's just not possible. But... what if? ;)
Matt
I will certainly take a look at it, it should be re-written anyways to do that. Not sure when I will get to it, but I will do my best to correct this sometime this week.
--Matt
Fred K
Cheers!
Matt
BS0D
I'm getting the following error message: "#1214 - The used table type doesn't support FULLTEXT indexes"
I don't know how to fix that... does that mean i already have fulltext set up, or just that I cannot set it up at all?
BS0D
I was a bit hesitant at first because I don't know much about mySQL and I'm not sure if it has an impact on the speed/consistency of the database. Didn't want my site to lose its functionalities or get it messed up!
BS0D
Only the last article appears with the related div underneath, but it hides all the previous articles in that category.
Did I miss something? Did I do something wrong?
Matt
Something didn't go quite right it seems, as the related articles should only show on an article when it's being viewed full-page.
Do you have a link, I'd like to take a look at the source? You can send it to me through the contact form if you don't want to post it publicly.
BS0D
I don't think that will be necessary, I fixed it myself. Quite simply actually... I just changed the line
to
It might be a horrible way to fix that, I am no PHP expert and only have the basics, but it works fine on my site. I don't seem to have lost any functionalities and have noticed no bugs so far (I tried that locally first of course) :)
If you can suggest a better fix of course I'll take it!
Matt
Jonathan
Does the mod implemented in your site uses same code or different ?
Would you mind sharing how to display the article image of related articles.
---Jonathan
Matt
This site is no longer running sNews CMS, though I do still use it. sNews CMS has no default way to associate an image with an article the way I have here.
A "hacky" way to do it, is to upload a .jpg thumbnail to your files directory, and name the file the id # of the article, then you could add it into the code, such like;
<?php if (file_exists('file_directory/'.$r['aid'].'.jpg')) { $list .= '<li><img src="file_directory/'.$r['aid'].'.jpg" /><a href="'._SITE.$link.$r['asef'].'/">'.$r['title'].'</a>'.$show_cat.'</li>'; } else { $list .= '<li><img src="file_directory/default.jpg" /><a href="'._SITE.$link.$r['asef'].'/">'.$r['title'].'</a>'.$show_cat.'</li>'; } ?>This is just an untested rough example, you'll need to wteak the HTML/CSS a bit to get it looking similar to mine. Don't forget to upload a default.jpg thumbnail in case the article doesn't have one associated.
Rakesh Jain
Thanks for your sNews Mods , They are wonderful.
I really appreciate your hard work.
There is small isse with "Light-weight related articles mod ", First link show in article page is always same as the article itself.
For Example :
On Article 1 , it shows the related articles as
Article1
Article2
Article3
Article4
Article5
Is there any ways you can exclude Article 1 is related article section ?
Matt
It shouldn't show the article you're currently reading. If you look on line 91 of the php file, it should be
that should exclude the ID of the article you're viewing.
Are you using sNews 1.7?