Related Stories section for Geeklog

In my view a related stories section is the only lacking feature of Geeklog, how hard is it to implement?
- Well let's find out

Lots of bugs fixed in the getStoryRelations method (2011-03-16)

Why bother?

If a user ends up on your site from a search engine or an external link, this will bring attention to the fact that you have other content that might prove useful.
It also may increase the ranking from various search engines. Links equals traffic.

Listing the stories

Before you start modifying anything, make sure you've read this entire article at least once.

The idea is simple, when adding a story in geeklog you can specify meta keywords, why not use those to find other stories with similar keywords and list those stories as relevant? It shouldn't be too hard to implement.

Let's also insert a crude "score" system for sorting the related topics by relevance. This is how I imagined it

  • Separate all keywords of the current story into an array
  • For each keyword, select all stories with the keyword in it.
  • If the story is already listed because of another keyword, increase the "relevance score"
  • Sounds simple enough so let's code!

     

    Coding

    I won't bother making this as a plugin or block, in fact I'm not a very good php programmer and don't know much about geeklog at all. Instead I will modify some of the core files in GeekLog. I'd much rather use a plugin but as I couldn't find any and all I want is to get my site up and running with a minimum of effort I'll just do it the "dirty" and "quick" way. Feel free to use this article as a reference or hint for developing a plugin. Don't worry if you spot a bug or performance issue, they are.. intended, but feel free to correct them and let me know of better ways to do it.

    Files

    I use the professional theme, in case you're running another theme you may have to adapt some of the filenames mentioned here.

    Download the following files

  • layout/professional/storytext.thtml
  • layout/professional/featuredstorytext.thtml
  • system/lib-story.php
  • Actually those are the only files I modified..

     

    lib-story.php

    I really can't explain the code, it's a lot of quirks and a severe case of "happy hacking".
    Paste the following code in an empty spot within this file:

     

     function array_sort($array, $on, $order=SORT_ASC)
    {
        $new_array = array();
        $sortable_array = array();
    
        if (count($array) > 0) {
            foreach ($array as $k => $v) {
                if (is_array($v)) {
                    foreach ($v as $k2 => $v2) {
                        if ($k2 == $on) {
                            $sortable_array[$k] = $v2;
                        }
                    }
                } else {
                    $sortable_array[$k] = $v;
                }
            }
    
            switch ($order) {
                case SORT_ASC:
                    asort($sortable_array);
                break;
                case SORT_DESC:
                    arsort($sortable_array);
                break;
            }
    
            foreach ($sortable_array as $k => $v) {
                $new_array[$k] = $array[$k];
            }
        }
    
        return $new_array;
    }
    
     function getStoryRelations($currentStoryId, $keyWords)
     { 
      global $_CONF, $_TABLES;
      $return = "";
      $keyWordArray = explode(",", $keyWords);
      $storyArray = null;
      $keyWord = "";  
      foreach ($keyWordArray as $keyWord)
      {	
    	$keyWord = trim($keyWord);			
    	if ($keyWord != "") {
    		$result = DB_query("select sid, `title` as storytitle from ".$_TABLES['stories']. " where sid<>'".$currentStoryId."' and draft_flag=0 and meta_keywords like '%".$keyWord."%' limit 0, 5");			
    		while ($dataRow = DB_fetchArray($result, true))
    		{
    		  $currentRow = $dataRow;	  
    		  $relStoryId = $currentRow['sid'];	  	  
    		  $relStoryTitle = $currentRow['storytitle'];	  	  
    		  $relStoryInfo = null;	  
    		  if (!isset($storyArray[$relStoryId]))
    		  { $relStoryInfo = array('title'=>$relStoryTitle, 'score'=>0, 'storyid'=>$relStoryId); }
    		  else
    		  {  $relStoryInfo = $storyArray[$relStoryId]; }
    		  $relStoryInfo['score'] = $relStoryInfo['score'] + 1;	     
    		  $storyArray[$relStoryId] = $relStoryInfo;	  	  	  
    		} 
    	} 
      }
      $storyArray = array_sort($storyArray, 'score', SORT_DESC); 
      $maxItems = 5;
      if (count($storyArray) > 0) {
          $return = '

    Related Articles

     

    • '; foreach($storyArray as $storyInfo) { if ($maxItems == 0) { break; } $maxItems --; $articleUrl = COM_buildUrl($_CONF['site_url'] . '/article.php?story='. $storyInfo['storyid']); $score = $storyInfo['score']; $return.='
    • '.$storyInfo['title']."
    • "; } $return.="

     

    "; } return $return; }

     

    Now you have the basic functionality in place and we only need to add the call to this method and set a variable on the $article object. Locate the line that says:

    if( $_CONF['hideviewscount'] != 1 ) {
    Inside the method STORY_renderArticle and insert a blank line above it.
    Insert the following line of code in the new blank line:
    $article->set_var( 'story_relations', getStoryRelations($story->getSid(), $story->DisplayElements('meta_keywords')) );

     

    Modifying the templates storytext.thml and featuredstorytext.html

    In my case I just want to list the relevant articles in direct connection with the body text of the story, as if I had entered them manually. With both of these files, locate the line that says

    {story_text_no_br}
    And insert this line:
    {story_relations}
    Directly in the line below.

     

    Done, upload the files to their original place and test it. Feel free to modify, improve, use and abuse this code.

    p.s

    You DID take a backup of the original files, right?

Page 1 /

 Facebook PHP SDK login problem

Have you integrated a facebook login to your site, or are you planning to? There\'s a small bug involving the sign in process that can make your life miserable unless you know how to get around it. Here\'s one possible solution.

 Thumbs Up Immerse VR Headset

This Immerse headset from ThumbsUp offers both positive and negative sides.

 Know your limits: Web Design

You might have the best app ever created, or you might have the user friendliest web page in the world. You might even have the most useful piece of shareware ever made and still not making any money.

 DroidXplorer big in China?

According to an email DroidXplorer will be preinstalled on devices, and be featured on a set of app stores. For free.

 Would this interest you?

Page 1 / 11