's in each page's header, such as rel="next" and rel="up", which allows some browsers to take advantage of this feature, by providing sequential and structured navigation for your weblog to the user.
Author: Remi Prevost
Author URI: http://exomel.com
*/
/* ===============================================================
- INSTALLATION -
1) Put this file on /wp-content/plugins/ directory.
2) Go to WordPress administration interface.
3) Get into the "Plugins" section.
4) Scroll to the "Relative links" row.
5) Click on the "Activate" link.
6) Enjoy.
- NOTES -
* This plugin won't work if you don't have
a permalink structure (using ?p=ID) or
a *regular* permalink structure. I tested
the plugin on various structures including those:
1) /%year%/%monthnum%/%day%/%postname%
2) /archives/%year%/%monthnum%/%day%/%postname%
3) /archives/%post_id%
4) /%postname%
It doesn't mean that it works "perfectly" on
3) and 4) but it won't cause any bugs. The
plugin was intended to be used on a 1) or 2)
permalink structure.
* A lot (a lot) of code bytes contained in
this file are the work of various
authors, especially WordPress contributors.
Thanks for letting me using them.
* This plugin is still in development; to see which
functions are not implemented yet, go to the To-do
section by searching for "==Todo" in the current document.
* Bug reports, comments, suggestions should be sent
to the author, Remi Prevost between
8am to 5pm EST on weekdays.
================================================================= */
/*============================
Function: Get relative post
============================*/
function relative_posts_get_relative_post($direction) {
global $wpdb, $post;
// If you change the "entry" string below,
// the plugin will use the specified term
// for a weblog 'entry'. You can change it
// to "post" if you want.
$whatwecallanentry = "entry";
$pp = false;
$nn = false;
if ($direction == "next") { $nn = true; }
else { $pp = true; }
$name = get_query_var('name');
$p = get_query_var('p');
// Removed support for ?p=ID permalinks. Get a webhost that supports .htaccess!
//if ($p) { $p = @$wpdb->get_row("SELECT ID, post_date FROM $wpdb->posts WHERE ID = '$p'"); }
$p = @$wpdb->get_row("SELECT ID, post_date FROM $wpdb->posts WHERE post_name = '$name'");
$current_post_date = $p->post_date;
$now = current_time('mysql');
$current_post_date = $p->post_date;
$row = null;
if ($pp) {
$row = @$wpdb->get_row("SELECT ID, post_title FROM $wpdb->posts WHERE post_date < '$current_post_date' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 1");
$linkrel = "previous";
$linkprefix = "Previous";
}
else {
$row = @$wpdb->get_row("SELECT ID,post_title FROM $wpdb->posts WHERE post_date > '$current_post_date' AND post_date < '$now' AND post_status = 'publish' AND ID != $p->ID ORDER BY post_date ASC LIMIT 1");
$linkrel = "next";
$linkprefix = "Next";
}
// halt if we didn't have something yet.
if (!$row) { return null; }
$link_url = get_permalink($row->ID);
$link_title = $row->post_title;
$link_title = apply_filters('the_title', $link_title);
$format = '';
echo $format."\n ";
}
/*============================
Function: Get relative month
============================*/
function relative_posts_get_relative_month($direction) {
global $wpdb, $m, $monthnum, $year, $month;
$monthnum = $_GET['monthnum'];
$year = $_GET['year'];
// Let's figure out when we are
if (!empty($monthnum) && !empty($year)) {
$thismonth = ''.zeroise(intval($monthnum), 2);
$thisyear = ''.intval($year);
} elseif (!empty($w)) {
// We need to get the month from MySQL
$thisyear = ''.intval(substr($m, 0, 4));
$d = (($w - 1) * 7) + 6; //it seems MySQL's weeks disagree with PHP's
$thismonth = @$wpdb->get_var("SELECT DATE_FORMAT((DATE_ADD('${thisyear}0101', INTERVAL $d DAY) ), '%m')");
} elseif (!empty($m)) {
$calendar = substr($m, 0, 6);
$thisyear = ''.intval(substr($m, 0, 4));
if (strlen($m) < 6) {
$thismonth = '01';
} else {
$thismonth = ''.zeroise(intval(substr($m, 4, 2)), 2);
}
} else {
$thisyear = gmdate('Y', current_time('timestamp') + get_settings('gmt_offset') * 3600);
$thismonth = gmdate('m', current_time('timestamp') + get_settings('gmt_offset') * 3600);
}
if ($direction == "previous") {
$row = @$wpdb->get_row("SELECT DISTINCT MONTH(post_date) AS month, YEAR(post_date) AS year
FROM $wpdb->posts
WHERE post_date < '$thisyear-$thismonth-01'
AND post_status = 'publish'
ORDER BY post_date DESC
LIMIT 1");
$linkrel = "previous";
$linkname = "Previous";
}
else {
$row = @$wpdb->get_row("SELECT DISTINCT MONTH(post_date) AS month, YEAR(post_date) AS year
FROM $wpdb->posts
WHERE post_date > '$thisyear-$thismonth-01'
AND MONTH( post_date ) != MONTH( '$thisyear-$thismonth-01' )
AND post_status = 'publish'
ORDER BY post_date ASC
LIMIT 1");
$linkrel = "next";
$linkname = "Next";
}
$monthlink = get_month_link($row->year, $row->month);
$themonth = $month[zeroise($row->month, 2)];
if ($row) {
echo ''."\n ";
}
}
/*============================
Function: Get absolute month
============================*/
function relative_posts_get_absolute_month($position) {
global $wpdb, $m, $monthnum, $year, $month;
$monthnum = $_GET['monthnum'];
$year = $_GET['year'];
if ($position == "first") {
$order = "ASC";
$linkrel = "first";
$linkname = "First";
}
else {
$order = "DESC";
$linkrel = "last";
$linkname = "Last";
}
$row = @$wpdb->get_row("SELECT DISTINCT MONTH(post_date) AS month, YEAR(post_date) AS year
FROM $wpdb->posts
WHERE post_status = 'publish'
ORDER BY post_date $order
LIMIT 1");
$monthlink = get_month_link($row->year, $row->month);
$themonth = $month[zeroise($row->month, 2)];
if ($row and ($monthnum != $row->month or $year != $row->year)) {
echo ''."\n ";
}
}
/*============================
Function: Get top directory
============================*/
function relative_posts_get_my_top_link() {
// Pretty simple, eh?
echo ''."\n ";
}
/*=============================
Function: Get parent directory
=============================*/
function relative_posts_get_my_up_link() {
global $wpdb, $m, $monthnum, $year, $month;
if (is_single() or is_archive() and !is_home() and !is_author() and !is_category() or is_page()) {
// Define variables of the requested URI.
$r = $_SERVER["REQUEST_URI"];
$h = $_SERVER["HTTP_HOST"];
$lastChar = substr($r,strlen($r) - 1,1);
// if the last caracter of the URI is not
// a slash, add it by ourself.
if ($lastChar != "/") { $r = $r . "/"; }
// omg. a regex!
$rm = 'http://' . $h . ereg_replace('(.*/)(.*)/',"\\1",$r);
// Try to get page's parent name
if (is_page()) {
$page = $_GET['pagename'];
// get the last part of the page path
// by removing everything before the
// last "/".
$page = ereg_replace("(.*)/",'',$page);
$row = @$wpdb->get_row("SELECT * from $wpdb->posts
WHERE post_status = 'static'
AND post_name = '$page'
ORDER BY post_date ASC
LIMIT 1");
$parent = $row->post_parent;
if ($parent) { $row2 = @$wpdb->get_row("SELECT * from $wpdb->posts
WHERE post_status = 'static'
AND ID = '$parent'
ORDER BY post_date ASC
LIMIT 1");
$page_title = $row2->post_title;
}
else {
// if page doesn't have a parent, it's
// because it's on the page first level.
$page_title = get_settings('blogname');
}
}
// Try to get the day
// if we're in a single post
if (is_single() and $_GET['monthnum']) {
$page_title = $month[zeroise($_GET['monthnum'], 2)];
if ($_GET['day']) { $page_title .= " ".$_GET['day']; }
$page_title .= " ".$_GET['year'];
}
// Try to get the month number
// if we're in a day
if (is_day()) {
$page_title = $month[zeroise($_GET['monthnum'], 2)];
$page_title .= " ".$_GET['year'];
}
// Try to get the year
// if we're in a month
if (is_month()) {
$page_title = $_GET['year'];
}
// Try to get the blog name
// if we're in a year
if (is_year()) {
// This depends on permalink structure...
// return null;
$page_title = get_settings('blogname');
}
echo '' ."\n ";
}
}
/*============================================
Function: Do all that stuff when it's called.
============================================*/
function relative_posts_do_all() {
// Doesn't work well with ?p=ID structure. Sorry.
if (get_settings('permalink_structure') == "") { return null; }
// Uncomment the following to get a nice HTML comment
// echo "\n ";
/* Basic functions */
relative_posts_get_my_top_link();
relative_posts_get_my_up_link();
/* Next and previous weblog entry */
if (is_single()) {
relative_posts_get_relative_post("previous");
relative_posts_get_relative_post("next");
}
/* Next and previous month */
if (is_month()) {
relative_posts_get_relative_month("previous");
relative_posts_get_relative_month("next");
}
/* First and last month */
if (is_month()) {
relative_posts_get_absolute_month("first");
relative_posts_get_absolute_month("last");
}
/* ==Todo
Yes, I'll do it when I'll have the time, although
some of them are quite useless.
-------------------------------------------------*/
// relative_posts_get_absolute_post("first");
// relative_posts_get_absolute_post("last");
// relative_posts_get_relative_day("next");
// relative_posts_get_relative_day("previous");
// relative_posts_get_absolute_day("first");
// relative_posts_get_absolute_day("last");
// relative_posts_get_relative_year("next");
// relative_posts_get_relative_year("previous");
// relative_posts_get_absolute_year("first");
// relative_posts_get_absolute_year("last");
}
/*====================================================
Filter: Run the main function when were in the
====================================================*/
add_filter('wp_head', 'relative_posts_do_all');
?>