Jump to content
Joss

Quick Tip: Related Articles

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

Share this post


Link to post
Share on other sites

Hey joss,

like that little hanna-snippet. But why are you using it in this situation? Would it be not better having a page reference field selecting the tags? 

Share this post


Link to post
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.

Share this post


Link to post
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 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By EyeDentify
      I as have been a bit confused for some time about how the "Markup Regions" functionality in Processwire worked.
      But i have know read a bit more and think that i am getting to grips with it. And Markup Regions is going to be huge.

      To aid me in understanding Markup Regions better i started to read the Source code for the new "Regular" theme in conjunction with the Blog about the markup regions.
      It helped me a great deal to understand the basics and more fine details of it.

      A tip is to open both links and use the Source code of the "Regular" theme while reading the blog post.
       
      The Source code:
      https://github.com/processwire/processwire/blob/dev/site-regular/templates/_main.php
      The Blog post:
      https://processwire.com/blog/posts/processwire-3.0.62-and-more-on-markup-regions/
       
      I hope this could help others starting out with markup regions.
      Just take it slow and read it a couple a times and soon you will see the greatness of markup regions.

      /EyeDentify 
    • By EyeDentify
      Hello Fellow forum members.

      I wanted to share two links to a guide and a cheatsheet concerning Crontab and Cronjobs.
      This is a result of me doing some research in how Cronjobs work and how to use it and i thought i share for other beginners use.

      So the guide that got me started and is a good reference is:
      A Comprehensive Crash Course Into Cronjobs (sitepoint)
      And also i found this sort of cheatsheet and database of cronjob configurations handy:
      Corntab - the Crontab GUI
      I hope these tips can help any beginners like myself get up and running with cronjobs.
       
    • By Guy Verville
      I am getting suddenly this error while trying to make some hannacode  (Setup/Hanna Code). My site in local work perfectly, but not on the production site. Any idea?
      Class 'ProcessWire\TextformatterHannaCode' not found in /...site/modules/TextformatterHannaCode/ProcessHannaCode.module:190
      Stack trace:
      #0 [internal function]: ProcessHannaCode->___execute()
      #1 /.../wire/core/Wire.php(347): call_user_func_array(Array, Array)
      #2 /.../wire/core/WireHooks.php(548): ProcessWire\Wire->_callMethod('___execute', Array)
      #3 /.../wire/core/Wire.php(370): ProcessWire\WireHooks->runHooks(Object(ProcessHannaCode), 'execute', Array)
      #4 /.../wire/core/ProcessController.php(240): ProcessWire\Wire->__call('execute', Array)
      #5 [internal function]: ProcessWire\ProcessController->___execute()
      #6 /.../wire/core/Wire.php(347): call_user_func_array(Array, Array)
      #7 /.../wire/core/WireHooks.php(548): ProcessWire\Wire->_callMethod('___execute', Array)
      #8 /.../wire/core/Wire.php(3 (line 190 of /home/guyvervi/public_html/site/modules/TextformatterHannaCode/ProcessHannaCode.module) 
       
      I am using the latest version of PW (3.27)
    • By Guy Verville
      I get this error on my site. I'm using the the 3.0.18 branch. Any idea?
      Error: Class 'ProcessWire\TextformatterHannaCode' not found (line 190 of /home/guyvervi/public_html/site/modules/TextformatterHannaCode/ProcessHannaCode.module) 
      This happens when I try to access Setup > Hanna Code
    • By Peter Knight
      I'd like to see better Hanna Code management and I think 4 things could greatly assist.
      1 - Tags
      to be able to tag a piece of Hanna Code in the same manner as we tag templates. We could then see the list of Hanna Codes arranged in the same format as templates. IE broken down by tag.
       
      2 - Usage
      On the Hanna Code list i'd also like to see a column called Pages (and Templates?) telling me how many pages and templates use my Hanna Code. Again, similar to the Templates layout.
       
      3 - Filter / Find
      The Find page has a tab called Filters for searching by template, field etc etc. In fact, I can filter by almost anything except Hanna Codes.
       
      4 - Descriptions
      To be able to add a plain text description to a Hanna Code. I try to name my HCs in a helpful way such as 
      Get-Video
      Get-Investor-News
      etc
      Sometimes I have several variations of the above and I'd like an at-a-glance way to distinguish them without having to click into the HC or at least be able to see the description once I've clicked in.
      Anyone else feel the same or do I use HC more than most?
×
×
  • Create New...