forked from adamfairholm/PyroCMS-Related-Posts-Plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Compatible with PyroCMS 2.x
- Loading branch information
0 parents
commit e1d61a7
Showing
2 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# PyroCMS Related Posts Plugin | ||
|
||
The PyroCMS Related Posts plugin allows you to show a list of blog post headlines that use specified keywords. | ||
|
||
The idea behind this is that you can effortlessly add a small lex-based line of code to your pages to show posts that are tagged with related keywords. | ||
|
||
So for example, if you wanted to show the 5 latest posts that use the keywords "news" and "technology", you would do this: | ||
|
||
{{ related_posts:posts keywords="news,technology" maximum_posts="5" maximum_length="100" }} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php defined('BASEPATH') OR exit('No direct script access allowed'); | ||
/** | ||
* Related Posts | ||
* | ||
* Fetches posts that are tagged with keyword(s) specified. | ||
* | ||
* @package PyroCMS | ||
* @author Rick Mills (WDWFans.com) <rick.sketchy@gmail.com> | ||
* @copyright Copyright (c) 2012 Rick Mills | ||
* | ||
*/ | ||
|
||
class Plugin_Related_Posts extends Plugin | ||
{ | ||
/** | ||
* Posts | ||
* | ||
* Usage: | ||
* {{ related_posts:posts keywords="keyword1,keyword2,keyword3,etc" maximum_posts="5" maximum_length="100" }} | ||
* | ||
* @return string (related posts are returned as part of a list and should be wrapped with 'ul' or 'ol' tags. | ||
*/ | ||
function posts() | ||
{ | ||
$keywords = $this->attribute('keywords'); | ||
$maxposts = $this->attribute('maximum_posts'); | ||
$maxlength = $this->attribute('maximum_length'); | ||
|
||
$keywordArray = explode(",", $keywords); | ||
|
||
$this->db->select('id'); | ||
$this->db->from('keywords'); | ||
$this->db->where_in('name', $keywordArray); | ||
$result = $this->db->get(); | ||
|
||
$keywordIds = array(); | ||
foreach($result->result() as $r) | ||
{ | ||
$keywordIds[] = $r->id; | ||
} | ||
|
||
if(!empty($result)) | ||
{ | ||
$this->db->select('*'); | ||
$this->db->from('keywords_applied'); | ||
$this->db->where_in('keyword_id', $keywordIds); | ||
$applied = $this->db->get(); | ||
|
||
if(!empty($applied)) | ||
{ | ||
$hash_store = array(); | ||
foreach($applied->result() as $a) | ||
{ | ||
$hash_store[] = $a->hash; | ||
} | ||
|
||
if(!empty($hash_store)) | ||
{ | ||
// time to search the blog posts! | ||
$this->db->select('*'); | ||
$this->db->from('blog'); | ||
$this->db->where_in('keywords', $hash_store); | ||
$this->db->limit($maxposts); | ||
$this->db->order_by('created_on', 'desc'); | ||
$postQuery = $this->db->get(); | ||
|
||
$postString = ''; | ||
|
||
foreach($postQuery->result() as $post) | ||
{ | ||
$postString .='<li>'.anchor('blog/'.date('Y/m', $post->created_on) .'/'.$post->slug, $this->neat_trim($post->title, $maxlength)).' - '.date('M jS Y', $post->created_on).'</li>'; | ||
} | ||
|
||
return $postString; | ||
} | ||
} | ||
} | ||
} | ||
|
||
function neat_trim($str, $n, $delim='...') | ||
{ | ||
$len = strlen($str); | ||
if ($len > $n) | ||
{ | ||
preg_match('/(.{' . $n . '}.*?)\b/', $str, $matches); | ||
return rtrim($matches[1]) . $delim; | ||
} | ||
else | ||
{ | ||
return $str; | ||
} | ||
} | ||
} | ||
/* End of file related_posts.php */ |