Jump to content

related posts


benbyf
 Share

Recommended Posts

I've created a tags field so that the CMS user may add tags to blog posts that then link to a list of all the posts with that tag:

Reading out tags from all post pages:

<?php
$matches = $pages->find("template=post");
$tags = array();
foreach($matches as $key => $tag){
  	
   foreach(explode(",", $tag->tags) as $key => $t) {
                  $tags[$t] = str_replace(' ','',$t);
   }
}
$tagsClean = array_unique($tags);
sort($tagsClean);

foreach($tagsClean as $word){

//if not blog page creat links to search from the blog
if($page->path == '/blog/'){
	echo '<a href="?topic=' . $word . '">';
}else{
	echo '<a href="../?topic=' . $word . '">';
}

echo $word;
echo '</a><br />';
} 

I'm now trying to work out how to list those pages which have the same tag as the current page (related posts):

as far it only lists what tags the current page does have:

<?php
$pageTags = array();
foreach(explode(",", $page->tags) as $key => $t) {
              $pageTags[$t] = str_replace(' ','',$t);
}
$pageTagsClean = array_unique($pageTags);
sort($pageTagsClean);

foreach($pageTagsClean as $pageTag){
echo '<a href="../?topic=' . $pageTag . '">';
echo $pageTag;
echo '</a><br />';
}

Any help would be great, thanks

Link to comment
Share on other sites

I tried something that should do what you want. Although it depends what you want to archive exactly, there are alternative ways/technics that would work in PW21.

<?php

$pagetags = explode(',',$page->tags);
$tagstring = implode('|',$pagetags);
$tagstring = substr($tagstring,0,strlen($tagstring));

echo $tagstring.'<br/>';

$related = $pages->find("template=page, tags~=$tagstring");

echo '<ul>';
foreach($related as $item){
if($item !== $page){
	echo "<li><a href='{$item->url}'>{$item->title}</a> (RelatedPageTags: {$item->tags})</li>";
}
}
echo '</ul>';

You may need to further limit the $pages->find by using more selectors and a limit etc, depending on how many pages there are.

Link to comment
Share on other sites

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...