0

I want to make an exception for my custom taxonomy order. Sometimes you need to put "Other" option as last option but when calling taxonomies with "orderby=name", sadly "Other" term is not listing as last option. Is there any proper way for this exception?

1
  • Is this about front or backend? – brasofilo Jun 14 '12 at 14:16
4
+50

Here is the example of get_terms in the Codex:

$terms = get_terms("my_taxonomy");
 $count = count($terms);
 if ( $count > 0 ){
     echo "<ul>";
     foreach ( $terms as $term ) {
       echo "<li>" . $term->name . "</li>";

     }
     echo "</ul>";
 }

Lets say there was an 'other' term in my_taxonomy. To place it at the end as you requested, we would check each term if it matched, and put it aside for safekeeping. Then, when the loop is finished, we would check if it was found and display it at the end.

e.g.

 $terms = get_terms("my_taxonomy");
 $count = count($terms);
 if ( $count > 0 ){
     echo "<ul>";
     $other_term = null;
     foreach ( $terms as $term ) {
       if($term->name == 'other'){
           $other_term = $term;
       } else {
           echo "<li>" . $term->name . "</li>";
       }
     }
     if($other_term != null){
       echo "<li>" . $other_term->name . "</li>";
     }
     echo "</ul>";
 }

Think of it as a queue, first in first out, only when the person called 'other' arrives at the window for processing, a guard grabs him and pulls him aside. When the queue is empty and everyone has been processed, the guard puts the 'other' person back in the queue at the end.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.