Created
July 10, 2013 09:46
-
-
Save nielsvr/5964980 to your computer and use it in GitHub Desktop.
Get previous and next term from a taxonomy in WordPress. Use in a Taxonomy Archive.
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 | |
$queried_object = get_queried_object(); // returns the current taxonomy term | |
$my_taxonomy = get_terms("my_taxonomy", | |
array( | |
'orderby' => 'name', | |
'order' => 'ASC' | |
) | |
); | |
$previous_tax = false; | |
$next_tax = false; | |
$last_tax_in_loop = false; | |
$passed_current = false; | |
if( $my_taxonomy && is_array( $my_taxonomy ) ) { | |
foreach($my_taxonomy AS $tax) { | |
if($queried_object->term_id == $tax->term_id) { | |
$passed_current = true; | |
if($last_tax_in_loop) { | |
$previous_tax = $last_tax_in_loop; | |
} | |
} else { | |
if($passed_current && !$next_tax) { | |
$next_tax = $tax; | |
} | |
} | |
$last_tax_in_loop = $tax; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment