|
<?php |
|
/* |
|
Plugin Name: tinyRelated |
|
Plugin URI: http://wordpress.org/plugins/tinyrelated/ |
|
Description: A tiny and simple related posts plugin. |
|
Author: Arūnas Liuiza |
|
Version: 0.2.1 |
|
Author URI: http://klausk.aruno.lt/ |
|
License: GPL2 |
|
|
|
Copyright 2014 Arūnas Liuiza (email : klausk@aruno.lt) |
|
|
|
This program is free software; you can redistribute it and/or modify |
|
it under the terms of the GNU General Public License, version 2, as |
|
published by the Free Software Foundation. |
|
|
|
This program is distributed in the hope that it will be useful, |
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
GNU General Public License for more details. |
|
|
|
You should have received a copy of the GNU General Public License |
|
along with this program; if not, write to the Free Software |
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|
*/ |
|
|
|
// block direct access to plugin file |
|
defined('ABSPATH') or die("No script kiddies please!"); |
|
|
|
// add filter to automatically show related posts after post content |
|
add_filter( 'the_content', array( 'tinyRelated', 'append_list') ); |
|
|
|
// main plugin class |
|
class tinyRelated { |
|
// options |
|
public static $options = array( |
|
'general_count' => 5, |
|
'general_show' => 'before', // before | after | false |
|
); |
|
|
|
// add list to the end of post_content |
|
public static function append_list( $content ) { |
|
if (is_singular('post')) { |
|
if ( self::$options['general_show'] ) { |
|
$post_id = get_the_id(); |
|
$related = self::build_list($post_id); |
|
switch ( self::$options['general_show'] ) { |
|
case 'after' : |
|
$content .= "\r\n".$related; |
|
break; |
|
case 'before' : |
|
$content = $related . "\r\n" . $content; |
|
break; |
|
} |
|
} |
|
} |
|
return $content; |
|
} |
|
|
|
// build html for related post list |
|
public static function build_list($post) { |
|
if (is_numeric($post)) { |
|
$post = get_post($post); |
|
} |
|
$posts = tinyRelated::get_list($post); |
|
$return .= apply_filters( 'tinyrelated_list_title', '<h3>'.'Related posts'.'</h3>' ); |
|
$return .= apply_filters( 'tinyrelated_list_start', '<ul class="tinyrelated">' ); |
|
foreach ($posts as $related_post) { |
|
if (is_numeric($related_post)) { |
|
$related_post = get_post($related_post); |
|
} |
|
if (!$related_post || $related_post->post_status != 'publish') { |
|
continue; |
|
} |
|
$return .= sprintf( |
|
apply_filters( 'tinyrelated_list_item', '<li><a href="%1$s">%2$s</a></li>' ), |
|
get_permalink($related_post), |
|
get_the_title($related_post) |
|
); |
|
} |
|
$return .= apply_filters( 'tinyrelated_list_end', '</ul>' ); |
|
return $return; |
|
} |
|
|
|
// get array of related posts - only dummy for now |
|
public static function get_list($post) { |
|
if (is_numeric($post)) { |
|
$post = get_post($post); |
|
} |
|
$args = array( |
|
'post_type' => $post->post_type, |
|
'post_status' => 'publish', |
|
'posts_per_page' => self::$options['general_count'], |
|
'orderby' => 'rand', |
|
); |
|
$return = new WP_Query($args); |
|
$return = $return->posts; |
|
$return = apply_filters( 'tinyrelated_get_list', $return, $post ); |
|
return $return; |
|
} |
|
} |
|
|
|
?> |