0

This is a very newbie question. But how would I sort the results of this alphabetically so the names in the li list are in order?

<?php 
// FOR THE CATEGORIES (PRODUCT TYPE)
$terms_one = get_terms('product_cat');
$count_one = count($terms_one);  
$i_one = 0; 

echo '<ul class="submenu one filter option-set" data-filter-group="gem-type">';
echo '<li><a href="javascript:void(0)" data-filter-value="" class="selected">ALL</a>';

if ($count_one > 0) {        
foreach ($terms_one as $term_one) 
{
    $i_one++;      
    $term_list_one .= '<li><a href="javascript:void(0)" data-filter-value=".'. $term_one->slug .'"> <span>'. $term_one->count .'</span>'. $term_one->name .'</a></li>';

    if ($count_one != $i_one) 
    {
        $term_list_one .= '';
        } else {
        $term_list_one .= '';
    }
} 
  echo $term_list_one;
}
echo '</ul>';
?> 

3 Answers 3

1

You can put in an array with arguments in the get_terms() function, orderby and order are probably what you want.

Here's the documentation:

http://codex.wordpress.org/Function_Reference/get_terms

Sign up to request clarification or add additional context in comments.

Comments

1
$terms_one = get_terms('product_cat','orderby=FIELD&order=ASC');

More information, you can refer to the documentation here: http://codex.wordpress.org/Function_Reference/get_terms

1 Comment

Thanks Jamie. That is what I was using. When it didn't work I thought I must be doing it wrong. I'm trying to sort jigoshop.com categories. Perhaps they do something tricky that doesn't allow to sort?
0

Try

asort($terms_one);

before doing the foreach loop.

If you want to sort by the array-key you can use ksort() ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.