Previous page : A bit of PHP
Next page : The first (a tad larger) bit of JavaScript on this site
A link between pages plugin
I found my previous solution to be slow and inflexible. What I instead did was to write my own little plugin. In the version 0.2 I succeeded with doing an fens post error, and miss the link in the next to last page. Sigh.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
<?php /* *Plugin Name: Previous Up Next - pun *Plugin URI: https://properhoc.com/programming/previous-up-next-pun/ *Description: Creates links between the current page, its parents and its nearest siblings. Add a [pun] shortcode (intended). It also add a [punno] shortcode. *Version: 0.3 *Author: Mauriz Blomqvist - properHoc *Author URI: https://properhoc.com */ //Adds the [pun] shortcode where the default texts before the links //are 'Up a level : ', 'Previous post : ','Next post : ' respectively. // The [punno] shortcode has no text as default. include('shortcodes.php'); function previous_up_next($previous_text=' ', $up_text=' ', $next_text=' '){ global $post; $previous_post=''; $parent_post=''; $next_post=''; $page_ID=get_the_ID(); // Get parent ID and post information - if any. if ($page_ID) { $parent_ID = wp_get_post_parent_id($page_ID); if ($parent_ID){ $parent_post=get_post($parent_ID); // Get list of siblings, i.e. a list of all pages with the same parent as the current page. $siblings=get_pages('child_of='. $parent_ID . '&parent='. $parent_ID. '&' . 'sort_column=menu_order&sort_order=asc'); $number_of_siblings=count($siblings); // Find the current page in the list of siblings if we have more than one child. if (1<$number_of_siblings){ for ($p=0; $p<$number_of_siblings; $p++) { if ($page_ID==$siblings[$p]->ID) break; } // If it is not the fist page, then retrieve the previous page. if (0<$p){ $previous_post=$siblings[$p-1]; } // If it is not the last page, then retrieve the next page. if ($p<$number_of_siblings-1){ $next_post=$siblings[$p+1]; } } } // Collect the output. // This will most likely be done in a somewhat more flexible way in the future. $output=''; if ($parent_post!=''){ $output.=$up_text.'<a href="'.get_permalink($parent_post->ID).'">'.$parent_post->post_title.'</a>'; } if ($previous_post!=''){ $output.='<br /> '.$previous_text.'<a href="'.get_permalink($previous_post->ID).'">'.$previous_post->post_title.'</a>'; } if ($next_post!=''){ $output.='<br /> '.$next_text.'<a href="'.get_permalink($next_post->ID).'">'.$next_post->post_title.'</a>'; } return $output; } } |
The shortcode part is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?php // Default is no text before link: add_shortcode('punno', 'next_up_previous_shortcode_no'); //Default is 'Up a level : ', 'Previous page : ','Next page : ' add_shortcode('pun', 'next_up_previous_shortcode'); function next_up_previous_shortcode($attributes) { extract(shortcode_atts(array( 'previous_text'=>'Previous post : ', 'up_text'=>'Up a level : ', 'next_text' => 'Next post : ' ), $attributes)); return previous_up_next($previous_text,$up_text,$next_text); } function next_up_previous_shortcode_no($attributes) { extract(shortcode_atts(array( 'previous_text'=>'', 'up_text'=>'', 'next_text' => '' ), $attributes)); return previous_up_next($previous_text,$up_text,$next_text); } |
I have also changed the functions.php file in a child of the Theme I am using. This adds shortcode to the the beginning and the end of each page. Those shortcodes will then be turned to calls of the previous_up_next function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' ); function theme_enqueue_styles() { wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' ); } add_filter('the_content','add_my_c1'); function add_my_c1($content) { $lineup='<a href="http://media.properhoc.com/2017/01/lineup.png"><img class="aligncenter size-full wp-image-391" src="https://media.properhoc.com/2017/01/lineup.png" alt="" width="654" height="3" /></a>'; $linedown='<a href="http://media.properhoc.com/2015/10/linelow.png"><img class="aligncenter size-full wp-image-409" src="https://media.properhoc.com/2015/10/linelow.png" alt="" width="654" height="3" /></a>'; $up_text = '[pun]'.$lineup; $low_text = $lineup.chr(0x0D).'[pun]'.$lineup.'Last modified: [last-modified]'; $content = $up_text.$content.$low_text; return $content; } ?> |
Up a level : Programming
Previous page : A bit of PHP
Next page : The first (a tad larger) bit of JavaScript on this siteLast modified: