Skip to content

Instantly share code, notes, and snippets.

@ideag
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ideag/5166752c1019cd0eb7b7 to your computer and use it in GitHub Desktop.
Save ideag/5166752c1019cd0eb7b7 to your computer and use it in GitHub Desktop.
v0.2.0
<?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.0
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_after' => true,
);
// add list to the end of post_content
public static function append_list( $content ) {
if (is_singular('post')) {
if ( self::$options['general_show_after'] ) {
$post_id = get_the_id();
$related = self::build_list($post_id);
$content .= "\r\n".$related;
}
}
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;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment