<?php
/*
Plugin Name: prev_next
Plugin URI: http://blog.elearning.co.jp
Description: Link to the previous page and the next page
Version: 1
Author: y.nishimura
Author URI: http://blog.elearning.co.jp
*/
/**
* this function returns previous page and next page
* if not exist, return false.
*/
function get_prev_next(){
static $pageArray; // for performance enhancement
if(!isset($pageArray))$pageArray = get_pages(‘sort_column=menu_order,post_title’);
foreach($pageArray as $page)$idArray[] = $page->ID;
$pos = array_search(get_the_ID(), $idArray);
$ret[‘prev’] = isset($pageArray[$pos – 1]) ? $pageArray[$pos – 1] : false;
$ret[‘next’] = isset($pageArray[$pos + 1]) ? $pageArray[$pos + 1] : false;
return $ret;
}
/* define shortcode [prev-page] and [next-page] */
function short_code_next(){
$prev_next = get_prev_next();
$next = $prev_next[‘next’];
if($next)return ‘<a href=”‘.get_page_link($next->ID).'”>’.htmlentities($next->post_title, ENT_QUOTES, ‘utf-8′).’ »</a>’;
}
function short_code_prev(){
$prev_next = get_prev_next();
$prev = $prev_next[‘prev’];
if($prev)return ‘<a href=”‘.get_page_link($prev->ID).'”>« ‘.htmlentities($prev->post_title, ENT_QUOTES, ‘utf-8′).'</a>’;
}
add_shortcode(‘prev-page’, ‘short_code_prev’);
add_shortcode(‘next-page’, ‘short_code_next’);
/* prepend link-unit to the “pages” (not to posts) */
function prev_next_prepender($content) {
if(is_page())$content = ‘<div><div style=”float:left”>[ prev-page ]</div> <div style=”float:right”>[ next-page ]</div></div><br/>’.$content;
return $content;
}
add_filter ( “the_content”, ‘prev_next_prepender’);
?>