Skip to content

Instantly share code, notes, and snippets.

@nielsvr
Created July 10, 2013 09:46
Show Gist options
  • Save nielsvr/5964980 to your computer and use it in GitHub Desktop.
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.
<?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