Jump to content

Quick Tip: Related Articles


Joss
 Share

Recommended Posts

Here is the scenario.

You have developed a blog or news site or blog section to your site and you have created a basic tag system using a Page Reference field.

How can you add related articles easily?

This is one way by using Hanna code. I like this method because it gives me the choice of showing or not showing related articles and choosing the tag.

My articles use a template called basic-page which includes a Page Reference field called tags. This is a multiple select field.

I have created a Hanna code called "tagsearch" and given it the attribute "tag". In my textarea field it is used thus:

[[tagsearch tag="fish"]]

The Hanna code simply searches the title field of the tags pages for the single term and returns the pages that have that tag. I have limited the results to 8.

From the results, we pluck the title field of the pages, the small image that I use for my thumbnail, and the url.

However, we do not want to also return the page we are displaying, so we simply eliminate it by making sure that that none of the results have the same page name.

Here is the commented Hanna code.

 

<?php
// Find the pages that use the specified tag
$articles = $pages->find("template=basic-page, tags.title=$tag, limit=8");

// Start the loop
foreach($articles as $article){
  
  // Check we are only displaying articles that are NOT the current page
    if($article->name != $page->name){
      
  // Add a thumbnail, but check it is there so we don't get errors
    if($article->image_small){
      echo "<a href='{$article->url}'><img src='{$article->image_small->url}'></a><br>";
    }
  
  // Grab the article title
    echo "<br><a href='{$article->url}'>{$article->title}</a>";
      
  // end the check to make sure we do not show the current page
    }
  
  // end the loop
}

And that is it.

  • Like 7
Link to comment
Share on other sites

I could, but this way means I can put it anywhere in the article and make more than one search. It would be easy enough to add an extra atribute to say how many you want returned.

Link to comment
Share on other sites

I've been doing something very similar -- might I suggest a small change to the page selector to eliminate the need to check for the current page in your foreach loop?

// Find the pages that use the specified tag -- and ensure the current page is excluded
$articles = $pages->find("template=basic-page, id!={$page->id}, tags.title=$tag, limit=8");

This way, you always get 8 results (if there are that many, of course), whereas excluding page from the loop would leave you with 7 pages.

 

 

  • Like 2
Link to comment
Share on other sites

  • 2 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...