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...related icon... (from pinvoke). That should be it, upload your modified stylesheet and then your modified snews.php and you should be all set.

You might like

Comments


Light-weight? Sure. But it does the max.
Another good shot sheriff.


Nice one, dude... one thing though... in the article, full one. The edit link disappears. I can only access edit through admin or if I'm in category, not in ful article... Can you duplicate ?


Hmmmm, are you certain you didn't accidentally edit/remove the lines that show the edit link?

I ask because the only thing this mod will do is show extra content, it's not changing any existing content.

Make sure you put the new code in the right spot BELOW that original code, and not remove any of that original code. This is the bit that displays the admin links;
} else if (_ADMIN) {
    echo '<p>'.$edit_link.'</p>';
}


Yupp, there still there:
					}
				} else if (_ADMIN) {
					echo '<p>'.$edit_link.'</p>';
				}
			} 
			//Related articles MOD
			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'); 
            }
			
			else if (empty($currentPage)) {...


That's very strange, have you looked at the page source to see if the edit link is there, just perhaps it's a styling issue?

You're always welcome to strip your DB info and email me your snews.php, and I can have a look... you have my email


I have, Matt.. Still a no show.. I'll have another go at it, Thanks.


Hi.
I tested this version. Unfortunately does not work on my website.
In comparison with the full version, I found such a difference, for example:
full version:

WHERE MATCH(a.title, a.text) AGAINST ('>>>Tony >>>Hawk >>>Ride Triki break Odblokowanie' IN BOOLEAN MODE)


light version:

WHERE MATCH(a.title, a.text,a.keywords_meta,a.description_meta) AGAINST ('Tony Hawk: Ride ')


I can not explain it in English, but the point is that in the full version searches for "Tony>>> Hawk>>> Ride Tricks Unlocking break" in the light version of "Tony Hawk Ride". And this is probably the reason that they do not appear on my site related articles.

How to fix this?


Hmmm, that's odd toolman, maybe it's the 50% threshold of the full text natural language search.

You can add IN BOOLEAN MODE to the search query and see if that helps.

i.e.

WHERE MATCH(a.title, a.text,a.keywords_meta,a.description_meta) AGAINST ('".$title.$tags."' IN BOOLEAN MODE)


Thx Matt. Now works. Would be useful even able to sort the results by relevance, because at the moment is sorted by category ID.


This should work to sort by relevance;
$query = "SELECT a.id AS aid,a.title,a.seftitle AS asef,a.text,a.category,a.published,a.visible,a.keywords_meta,c.id,c.name,c.seftitle AS csef,c.published,c.subcat AS subcat,x.id,x.name AS xname,x.seftitle AS xsef
,MATCH(a.title, a.text,a.keywords_meta,a.description_meta) AGAINST ('".$title.$tags."' IN BOOLEAN MODE) AS relevance
FROM "._PRE."articles AS a
LEFT OUTER JOIN "._PRE."categories AS c
    ON category = c.id AND c.published ='YES'
LEFT OUTER JOIN "._PRE."categories AS x
    ON c.subcat =  x.id AND x.published ='YES'
WHERE MATCH(a.title, a.text,a.keywords_meta,a.description_meta) AGAINST ('".$title.$tags."' IN BOOLEAN MODE)
 AND a.id != '".$_ID."'
 AND a.position IN ('1','3')
 AND date <= '".$now."' 
 AND a.published='1'
 AND a.visible='YES' 
 AND a.category=c.id
 AND date <= '".$now."'
 ".$require_cat."
ORDER BY relevance DESC LIMIT ".$post_limit."";


Huge thanks. Now works as I like :)

Yet only a small change

$ show_cat = '('. $ r [ 'csef'].')';

at

$ show_cat = '('. $ r [ 'name'].')';


and perfectly. :D


Good deal toolman, glad it's working well for you.


Download broken?


Hmmm, it works for me Amak? It's a direct link to the file, no fancy download script or anything...

http://mdj.us/media/mod.related-light1.0.2...

Let me know if it doesn't work for you still, I can email you the file.


Hi Matt, great work on sNews titbits, unbelievable.

Any chance I could make a request as you seem to be "heavily in the know" with sNews, I am desperate for a multilingual site, any suggestions? how to go about it etc, it seems to be the one thing that is really lacking for this application and I think it would make it even greater

Many thanks


Hi Simon,

Thanks for the comments. As for a multilingual sNews, are you basically looking for something like letting your users be able to choose which language file to use?

I imagine it would be fairly easy to make a multiligual system that select the language file based on a user preference as set in a cookie.


I mean a full system, for example (and I know it's a bad one), osCommerce has a great feature, they have it so when you insert a product you can insert it in the languages you choose and then on the front-end you can choose the language you wish to see the whole site in.

I mean, their form is as follows:

Product insert:

Field 1: Product Name: English
Field 2: Product Name: Spanish
Field 3: Product Name: German

Field 4: Description: English
Field 5: Description: Spanish
Field 6: Description: German

And so on, thus meaning that on the same admin page (Add Product or "Page")you can add the description for the same "Page" in the languages you choose for your website.

The reason I ask all this is due to the fact that I live and work in Spain and I need a website that has the option to have multilingual capability due to my audience, I do the translation as I am fluent in Spanish so there's no problem there.


Forgot to mention:

I love this software because it is so simple, I don't want to make it more complex, I think that there would be a wider market if it had this functionality, it is so easy to work with that I feel this is the only thing missing, oh, that and a robust shopping cart (hint, hint, wink, wink!).

Thanks for your time again Matt, really appreciate what you are doing for this software!


Simon, I get what you're saying. To do it properly, you'd need to have separate fields for everything except the seftitles. The article text, meta keywords, descriptions, even the category titles should likely need to be internationalized.

That would require quite a bit of re-work.

A shopping cart mod? lol, hmmm, you know, I've only once toyed with a shopping cart, and I wrote that in Perl more than 5 years ago.

I'm not sure I'd know how to start, I know at a higher end you'd need to integrate shipping/tracking systems, payment gateways, inventories, etc.

What types of items would you sell, physical, digital delivery?


Thanks for the feedback Matt.

I believe t would just be beneficial, there are a few shopping carts out there but mainly for older editions of sNews, and there's no way to get them to work with 1.7 versions as the code is very different.

One thing I just wanted to point out was the multilingual side of things, here's a link to a package for 1.6.3 but I never got it to work so ...

Here's the link anyway:

http://snewscms.com/forum/index.php?topic=...

I like the idea but it seems to run off of multiple snews files, one person claims his version actually runs off 1 snews file and he has put a link to his website to show, but as always, when someone get's it to work, they keep it to themselves, unlike the rest of us, shame!

Thanks again for your attention.


Wow, that is quite the mod to be honest. I agree that if done right, there's no reason you should have more than one snews.php file.

However, I can tell you to do that would require modifying just about every core function within sNews. It really should be a separate project fork.

A shopping cart is an interesting idea. I may have a look at some existing carts and see how they're done.


Nice tutorial, it works like a charm.
How can I modify it to display the related articles in the sidebar on the article page?

Thanks.


Hi James,

I would think that if you add the "display code" in the sidebar on the index.php template file, then it would show them where ever you place it, provided it's after the article has been called and the variables exist.

It should be the same code;

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'); 
}


Let me know if that doesn't work.


Thanks, unfortunately it won't work when I place this in the sidebar:

<h3>Related Articles</h3>
<ul>
<?php
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');
}
?>
</ul>


Ahhh, yeah, variable scope issue, that makes sense actually.

To do what you're asking James, we'd have to grab the SEFtitle or ID and query the DB to get the title and tags.

I'll have a look at maybe wrapping it all up into a separate sidebar function. Give me a day or two and I'll see what I can cook up.


Comments are closed. No new comments allowed.