Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit 306c7bad3afe3a1aab163b4e532ded9004788764 0 parents
@matthiassiegel authored
112 README
@@ -0,0 +1,112 @@
+== Related
+
+A simple 'related posts' plugin that lets you choose the related posts yourself instead of generating the list automatically.
+
+== Description
+
+A simple 'related posts' plugin that lets you choose the related posts yourself instead of generating the list automatically.
+
+Features:
+
+* Add related posts to your blog posts
+* Choose the related posts yourself
+* Custom markup possible, or simply use the default output
+* Re-order related posts via drag and drop
+
+I wrote the plugin for my own blog, where I wanted to have the option to add related posts to each blog post using a simple
+but functional plugin without a lot of junk. Unlike other 'related posts' plugins that use algorithms to automatically
+generate a list of posts, I wanted to be able to select the related posts myself.
+
+The plugin targets at small and medium sized blogs. On blogs with lots of posts (500+) it might not be very comfortable to
+choose the posts with the select box.
+
+At the moment I don't plan any improvements, but if you find the plugin useful and require a certain feature or
+improvement, just let me know.
+
+To display the related posts, simply add
+
+ <?php echo $related->show(get_the_ID()); ?>
+
+to your template, inside the Wordpress loop.
+For advanced options, see the installation docs.
+
+== Installation
+
+**Option 1 - Automatic install**
+
+Use the plugin installer built into Wordpress to search for the plugin. Wordpress will then download and install it for you.
+
+**Option 2 - Manual install**
+
+1. Make sure the files are within a folder.
+2. Copy the whole folder inside the wp-content/plugins/ folder.
+3. In the backend, active the plugin. You can now select related posts when you create or edit blog posts.
+
+**How to display the related posts on your website**
+
+The related posts are displayed by adding
+
+ <?php echo $related->show($post_id); ?>
+
+to your template. Replace `` $post_id `` with a post ID. If you call it within the Wordpress loop, you can use
+
+ <?php echo $related->show(get_the_ID()); ?>
+
+You have the option of either outputting a pre-formatted list or returning a PHP array of related posts to customise the
+markup yourself.
+
+**Examples**
+
+*Example 1: Using the default output*
+
+ <?php echo $related->show(get_the_ID()); ?>
+
+This can be called within the Wordpress loop. It will output a `` <ul> `` list with links.
+
+*Example 2: Returning an array*
+
+ <?php $rel = $related->show(get_the_ID(), true); ?>
+
+With the second argument set to true, it will return an array of post objects. Use it to generate your own custom markup.
+Here is an example:
+
+ <?php
+ $rel = $related->show(get_the_ID(), true);
+
+ // Display the title of each related post
+ foreach ($rel as $r) :
+ echo $r->post_title . '<br />';
+ endforeach;
+ ?>
+
+== Frequently Asked Questions
+
+= Who should use this plugin?
+
+People who want to list 'related posts' in their blog posts and want to choose the related posts themselves, instead of
+having a list generated automatically using algorithms like other plugins do.
+
+= Where does the plugin store its data?
+
+Data is stored in the postmeta table in the Wordpress database. No additional tables are needed.
+
+= How many related posts can I add?
+
+As many as you like, there's no limit.
+
+= Wordpress version X.X is listed as minimum required version. Any chance it will work with earlier versions?
+
+There's a good chance it will work with earlier versions, but I haven't tested it.
+
+= I have 500+ posts on my blog and selecting the posts with the one select box isn't very comfortable, lots of scrolling.
+
+That's true, it isn't ideal for large blogs. This might get improved in future releases, feel free to submit ideas.
+
+== Screenshots
+
+1. Choosing related posts in the edit post screen
+
+== Changelog
+
+= 1.0
+* Initial release. No known issues.
0  index.html
No changes.
118 readme.txt
@@ -0,0 +1,118 @@
+=== Related ===
+Contributors: chipsandtv
+Donate link: http://chipsandtv.com/
+Tags: related posts, related, post
+Requires at least: 2.9
+Tested up to: 2.9.1
+Stable tag: trunk
+
+A simple 'related posts' plugin that lets you choose the related posts yourself instead of generating the list automatically.
+
+== Description ==
+
+A simple 'related posts' plugin that lets you choose the related posts yourself instead of generating the list automatically.
+
+Features:
+
+* Add related posts to your blog posts
+* Choose the related posts yourself
+* Custom markup possible, or simply use the default output
+* Re-order related posts via drag and drop
+
+I wrote the plugin for my own blog, where I wanted to have the option to add related posts to each blog post using a simple
+but functional plugin without a lot of junk. Unlike other 'related posts' plugins that use algorithms to automatically
+generate a list of posts, I wanted to be able to select the related posts myself.
+
+The plugin targets at small and medium sized blogs. On blogs with lots of posts (500+) it might not be very comfortable to
+choose the posts with the select box.
+
+At the moment I don't plan any improvements, but if you find the plugin useful and require a certain feature or
+improvement, just let me know.
+
+To display the related posts, simply add
+
+ <?php echo $related->show(get_the_ID()); ?>
+
+to your template, inside the Wordpress loop.
+For advanced options, see the installation docs.
+
+== Installation ==
+
+**Option 1 - Automatic install**
+
+Use the plugin installer built into Wordpress to search for the plugin. Wordpress will then download and install it for you.
+
+**Option 2 - Manual install**
+
+1. Make sure the files are within a folder.
+2. Copy the whole folder inside the wp-content/plugins/ folder.
+3. In the backend, active the plugin. You can now select related posts when you create or edit blog posts.
+
+**How to display the related posts on your website**
+
+The related posts are displayed by adding
+
+ <?php echo $related->show($post_id); ?>
+
+to your template. Replace `` $post_id `` with a post ID. If you call it within the Wordpress loop, you can use
+
+ <?php echo $related->show(get_the_ID()); ?>
+
+You have the option of either outputting a pre-formatted list or returning a PHP array of related posts to customise the
+markup yourself.
+
+**Examples**
+
+*Example 1: Using the default output*
+
+ <?php echo $related->show(get_the_ID()); ?>
+
+This can be called within the Wordpress loop. It will output a `` <ul> `` list with links.
+
+*Example 2: Returning an array*
+
+ <?php $rel = $related->show(get_the_ID(), true); ?>
+
+With the second argument set to true, it will return an array of post objects. Use it to generate your own custom markup.
+Here is an example:
+
+ <?php
+ $rel = $related->show(get_the_ID(), true);
+
+ // Display the title of each related post
+ foreach ($rel as $r) :
+ echo $r->post_title . '<br />';
+ endforeach;
+ ?>
+
+== Frequently Asked Questions ==
+
+= Who should use this plugin? =
+
+People who want to list 'related posts' in their blog posts and want to choose the related posts themselves, instead of
+having a list generated automatically using algorithms like other plugins do.
+
+= Where does the plugin store its data? =
+
+Data is stored in the postmeta table in the Wordpress database. No additional tables are needed.
+
+= How many related posts can I add? =
+
+As many as you like, there's no limit.
+
+= Wordpress version X.X is listed as minimum required version. Any chance it will work with earlier versions? =
+
+There's a good chance it will work with earlier versions, but I haven't tested it.
+
+= I have 500+ posts on my blog and selecting the posts with the one select box isn't very comfortable, lots of scrolling. =
+
+That's true, it isn't ideal for large blogs. This might get improved in future releases, feel free to submit ideas.
+
+== Screenshots ==
+
+1. Choosing related posts in the edit post screen
+
+== Changelog ==
+
+= 1.0 =
+* Initial release. No known issues.
222 related.php
@@ -0,0 +1,222 @@
+<?php
+/*
+Plugin Name: Related
+Plugin URI: http://chipsandtv.com/
+Description: Simple related posts plugin
+Version: 1.0
+Author: Matthias Siegel
+Author URI: http://chipsandtv.com/
+
+
+Copyright 2010 Matthias Siegel (email : chipsandtv@gmail.com)
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+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
+*/
+
+
+
+if (!class_exists('Related')) :
+ class Related {
+
+ // Constructor
+ public function __construct() {
+
+ // Set some helpful constants
+ $this->defineConstants();
+
+
+ // Register uninstall hook
+ register_uninstall_hook(dirname(__FILE__) . '/related.php', array(&$this, 'uninstall'));
+
+ // Register hook to save the related posts when saving the post
+ add_action('save_post', array(&$this, 'save'));
+
+ // Start the plugin
+ add_action('admin_menu', array(&$this, 'start'));
+ }
+
+
+ // Defines a few static helper values we might need
+ protected function defineConstants() {
+
+ define('RELATED_VERSION', '1.0');
+ define('RELATED_HOME', 'http://chipsandtv.com/');
+ define('RELATED_FILE', plugin_basename(dirname(__FILE__)));
+ define('RELATED_ABSPATH', str_replace('\\', '/', WP_PLUGIN_DIR . '/' . plugin_basename(dirname(__FILE__))));
+ define('RELATED_URLPATH', WP_PLUGIN_URL . '/' . plugin_basename(dirname(__FILE__)));
+ }
+
+
+ // Removes all related post entries from the postmeta table
+ protected function uninstall() {
+
+ global $wpdb;
+
+ $wpdb->query($wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE meta_key = 'related_posts'"));
+ }
+
+
+ // Main function
+ public function start() {
+
+ // Load the scripts
+ add_action('admin_print_scripts', array(&$this, 'loadScripts'));
+
+ // Load the CSS
+ add_action('admin_print_styles', array(&$this, 'loadCSS'));
+
+ // Adds a meta box for related posts to the posts screen
+ add_meta_box('post-meta-boxes', 'Related posts', array(&$this, 'displayMetaBox'), 'post', 'normal', 'high');
+
+ }
+
+
+ // Load Javascript
+ public function loadScripts() {
+
+ wp_enqueue_script('jquery-ui-core');
+ wp_enqueue_script('jquery-ui-sortable');
+ wp_enqueue_script('related-scripts', RELATED_URLPATH .'/scripts.js', false, RELATED_VERSION);
+ }
+
+
+ // Load CSS
+ public function loadCSS() {
+
+ wp_enqueue_style('related-css', RELATED_URLPATH .'/styles.css', false, RELATED_VERSION, 'all');
+ }
+
+
+ // Save related posts when saving the post
+ public function save() {
+
+ global $wpdb;
+
+ if (!empty($_POST['related-posts'])) :
+ $id = $_POST['post_ID'];
+ $related = $_POST['related-posts'];
+
+ // Add or update related posts entry
+ update_post_meta($id, 'related_posts', $related);
+ endif;
+ }
+
+
+ // Creates the output on the post screen
+ public function displayMetaBox() {
+
+ global $post_ID;
+
+ echo '<div id="related-posts">';
+
+ // Get related posts if existing
+ $related = get_post_meta($post_ID, 'related_posts', true);
+
+ if (!empty($related)) :
+ foreach($related as $r) :
+ $p = get_post($r);
+ echo '
+ <div class="related-post" id="related-post-' . $r . '">
+ <input type="hidden" name="related-posts[]" value="' . $r . '" />
+ <span class="related-post-title">' . $p->post_title . '</span>
+ <a href="#">Delete</a>
+ </div>';
+ endforeach;
+ endif;
+
+ echo '
+ </div>
+ <p>
+ <select id="related-posts-select" name="related-posts-select">
+ <option value="0">Select</option>';
+
+ $query = array(
+ 'posts_per_page' => -1,
+ 'what_to_show' => 'posts',
+ 'nopaging' => true,
+ 'post_status' => 'publish'
+ );
+ $p = new WP_Query($query);
+ if ($p->have_posts()) :
+ while ($p->have_posts()) :
+ $p->the_post();
+ echo '
+ <option value="' . get_the_ID() . '">' . get_the_title() . '</option>';
+ endwhile;
+ endif;
+ wp_reset_query();
+
+ echo '
+ </select>
+ </p>
+ <p>
+ Select related posts from the list. Drag selected ones to change order.
+ </p>';
+ }
+
+
+ // The frontend function that is used to display the related post list
+ public function show($id, $return = false) {
+
+ global $wpdb;
+
+ if (!empty($id) && is_numeric($id)) :
+ $related = get_post_meta($id, 'related_posts', true);
+
+ if (!empty($related)) :
+ $rel = array();
+ foreach ($related as $r) :
+ $p = get_post($r);
+ $rel[] = $p;
+ endforeach;
+
+ // If value should be returned as array, return it
+ if ($return) :
+ return $rel;
+
+ // Otherwise return a formatted list
+ else :
+ $list = '<ul class="related-posts">';
+ foreach ($rel as $r) :
+ $list .= '<li><a href="' . get_permalink($r->ID) . '">' . $r->post_title . '</a></li>';
+ endforeach;
+ $list .= '</ul>';
+
+ return $list;
+ endif;
+ else :
+ return false;
+ endif;
+ else :
+ return 'Invalid post ID specified';
+ endif;
+
+ }
+ }
+
+
+
+endif;
+
+
+
+// Start the plugin
+
+global $related;
+
+$related = new Related();
+
+
+?>
BIN  screenshot-1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 scripts.js
@@ -0,0 +1,23 @@
+jQuery(document).ready(function($) {
+
+ $('#related-posts-select').change(function() {
+ var container = $('#related-posts');
+ var id = $(this).val();
+ var title = this.options[this.options.selectedIndex].text;
+
+ if ($('#related-post-' + id).length == 0) {
+ container.prepend('<div class="related-post" id="related-post-' + id + '"><input type="hidden" name="related-posts[]" value="' + id + '" /><span class="related-post-title">' + title + '</span><a href="#">Delete</a></div>');
+ }
+ });
+
+ $('.related-post a').live('click', function() {
+ $(this).parent().css('background-color', '#ff0000');
+ $(this).parent().fadeOut('normal', function() {
+ $(this).remove();
+ });
+ return false;
+ });
+
+ $('#related-posts').sortable();
+
+});
18 styles.css
@@ -0,0 +1,18 @@
+.related-post {
+ font-size: 11px;
+ margin: 6px 6px 8px;
+ padding: 8px;
+ background: #f1f1f1;
+ border: 1px solid #dfdfdf;
+ cursor: pointer;
+
+ border-radius: 4px;
+ -webkit-border-radius: 4px;
+ -moz-border-radius: 4px;
+}
+.related-post-title {
+ padding: 0 30px 0 0;
+}
+.related-post a {
+ float: right;
+}
Please sign in to comment.
Something went wrong with that request. Please try again.