Created
September 9, 2011 22:38
-
-
Save billerickson/1207505 to your computer and use it in GitHub Desktop.
Determine which type of prev/next post navigation to display based on source
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* functions.php | |
* | |
*/ | |
// Use single template file for Design Gallery archive and Taxonomy | |
add_filter('template_include', 'be_design_gallery_template', 1, 1); | |
function be_design_gallery_template($template) { | |
if( is_tax( 'template-type' ) ) $template = dirname(__FILE__) . '/archive-template.php'; | |
return $template; | |
} | |
/** | |
* archive-template.php | |
* | |
*/ | |
// Save the page | |
session_start(); | |
if (is_tax()) $_SESSION['original'] = get_query_var('template-type'); | |
else $_SESSION['original'] = 'all'; | |
/** | |
* single-template.php | |
* | |
*/ | |
// Start Session to receive previous page info | |
session_start(); | |
if(!isset($_SESSION['original'])) $_SESSION['original'] = 'all'; | |
// Put this where you want the navigation to appear | |
be_post_navigation(); | |
/** | |
* Post Navigation (included from /lib/functions/design-template-functions.php ) | |
* Uses the plugin 'previous-and-next-post-in-same-taxonomy'. See plugin for more information | |
* @link http://wordpress.org/extend/plugins/previous-and-next-post-in-same-taxonomy/ | |
* | |
*/ | |
function be_post_navigation() { | |
// Get Previous/Next Post Data | |
// -- If patch is active | |
if (function_exists('be_get_previous_post')) { | |
if ($_SESSION['original'] == 'all') { | |
$previous = be_get_previous_post(false, '', ''); | |
$next = be_get_next_post(false, '', ''); | |
} else { | |
$previous = be_get_previous_post(true, '', 'template-type'); | |
$next = be_get_next_post(true, '', 'template-type'); | |
} | |
// -- If patch is inactive | |
}else{ | |
if ($_SESSION['original'] == 'all') { | |
$previous = get_previous_post(false, '', ''); | |
$next = get_next_post(false, '', ''); | |
} else { | |
$previous = get_previous_post(true, '', 'template-type'); | |
$next = get_next_post(true, '', 'template-type'); | |
} | |
} | |
// Display Previous | |
if (!empty($previous)) { | |
echo '<a href="'.get_permalink($previous->ID).'" class="link-prev">'; | |
$image_ids = array_keys( get_children( array( 'post_parent' => $previous->ID, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'menu_order', 'order' => 'ASC' ) ) ); | |
$image = wp_get_attachment_image_src( $image_ids[0], 'template-nav'); | |
echo '<img src="'.$image[0].'" alt="" />'; | |
echo '<span>'.get_the_title($previous->ID).'</span></a>'; | |
} | |
// Display Next | |
if (!empty($next)) { | |
echo '<a href="'.get_permalink($next->ID).'" class="link-next">'; | |
$image_ids = array_keys( get_children( array( 'post_parent' => $next->ID, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'menu_order', 'order' => 'ASC' ) ) ); | |
$image = wp_get_attachment_image_src( $image_ids[0], 'template-nav'); | |
echo '<img src="'.$image[0].'" alt="" />'; | |
echo '<span>'.get_the_title($next->ID).'</span></a>'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This was written for a client project, and posted in response to a message on the wordpress-austin mailing list. Here's my response that goes with the code:
This is actually the exact issue I had and what resulted in that plugin. I had a client's site that had a bunch of portfolio items. If they were on the portfolio archive and clicked a project, they wanted previous/next to be from all the projects. If they were on a portfolio category and clicked a project, they wanted previous/next to be from that specific category.
WordPress doesn't support previous/next posts from custom taxonomies, which is why I wrote a patch ( http://core.trac.wordpress.org/ticket/17807 ) and turned it into a plugin ( http://wordpress.org/extend/plugins/previous-and-next-post-in-same-taxonomy/ ).
Your specific situation requires more code than simply the plugin, but luckily I've already written it :) I use a session variable to track where the user came from, and then display an appropriate prev/next link.
First, I created a single template file (archive-template.php because the post type is called "template") which is used both for the "all" archive and the taxonomy archives. In order to make WordPress use that for taxonomies, I'm using the template_include filter:
// Use single template file for Design Gallery archive and Taxonomy
add_filter('template_include', 'be_design_gallery_template', 1, 1);
function be_design_gallery_template($template) {
if( is_tax( 'template-type' ) ) $template = dirname(FILE) . '/archive-template.php';
return $template;
}
Inside that template file you can use is_tax() or !is_tax() to selectively code things for whether or not you're on a taxonomy page.
At the top, I want to save the page into a session variable. If we're on a taxonomy page, I set 'original' equal to the taxonomy slug. If we're not, I set it to 'all'.
// Save the page
session_start();
if (is_tax()) $_SESSION['original'] = get_query_var('template-type');
else $_SESSION['original'] = 'all';
Finally, on the single-template.php file at the top I start the session so I can get the value from the previous page. If there is no value (ex: they came from a direct link to that single post) it sets the value to 'all'.
// Start Session to receive previous page info
session_start();
if(!isset($_SESSION['original'])) $_SESSION['original'] = 'all';
I then created a function, be_post_navigation() that first checks to see that the plugin is active ( if/when my patch is added to core, the plugin can be disabled and this will work with core). Then it checks the session variable and uses the appropriate function for getting previous and next. Instead of a simple previous/next link I showed the post title and thumbnail, so I'm using be_get_previous_post() and be_get_next_post() to load the actual post, but you could use whatever function you wanted.