» Download the Plugin | Support Forum
WordPress provides functions to add previous/next links to your posts. You can use the 'in_same_cat' parameter to limit the results to posts in the same category.
This is great for posts, but when you're working with custom post types and taxonomies you lose this functionality, since 'in_same_cat' is limited to the taxonomy 'category'. I've written a patch for WordPress that fixes this issue. If the patch is accepted it will be my first official contribution to WordPress core.
I've built the patch into a plugin so you can easily test and use it on your website.
- Download the plugin from WordPress.org and activate it
- Add the functions you'd like to test to your theme files.
- Let me know how you're using it and if it works (or doesn't) in the comments
I'm using the plugin right now at the bottom of my portfolio items ( example ). Here's the code I'm using to render that box:
[php]
function be_single_project_navigation() {
// If the plugin is ever disabled, this disables the code
if( function_exists('be_previous_post_link') ):
echo '<div class="project-navigation">';
echo '<span class="title">Browse projects in the '.be_first_term('price-range').' range:</span>';
be_previous_post_link('<span class="previous">%link</span>', '« %title', true, '', 'price-range');
be_next_post_link('<span class="next">%link</span>', '%title »', true, '', 'price-range');
echo '</div>';
endif;
}
// A useful little function for getting the first tax term's name.
function be_first_term($taxonomy) {
global $post;
$terms = get_the_terms( $post->ID, $taxonomy );
$show = true;
foreach ($terms as $term) {
if ($show) return $term->name;
$show = false;
}
}
[/php]
Here are the functions that have been modified to use taxonomies:
- be_previous_post_link()
- be_next_post_link()
- be_adjacent_post_link()
- be_get_previous_post()
- be_get_next_post()
- be_get_adjacent_post()
- be_get_adjacent_post_rel_link()
- be_adjacent_posts_rel_link()
- be_next_post_rel_link()
- be_prev_post_rel_link()
- be_get_boundary_post()
- be_get_boundary_post_rel_link()
- be_start_post_rel_link()
Refer to the source of the plugin or WordPress Codex for documentation. They should work just like the original functions, except they now accept an additional parameter, $taxonomy, that's set to 'category' by default.
If you have any issues or implementation questions, please leave a comment. Also share your successful implementations! I'm just as much interested in seeing how it's used as I am in finding bugs.
I think this is a very useful addition and would love to see it in WordPress core. But even if it doesn't make it, I'll continue to maintain the plugin. I'm using it on my site and a client's already.
No comments yet