Forums » Sorting a Tag Cloud

RSS feed for this topic

Info

  1. I'm trying to sort and display the 50 most popular tags alphabetically. Currently I have

    <?php
    $tags = get_tags(array('sort' => 'most'), 50);
    sort($tags);
    echo tag_cloud($tags, uri('items/browse'));
    ?>

    This sort of works, it arranges the tags alphabetically, but it does so in two separate groups. The first group is tags starting with a capital letter and the second group is tags with a lower case letter.

  2. That's to be expected, since sorting treats upper and lower cases separately.

    It might work to get the effect to uppercase the name of the tag:

    <?php
    function ucTags($record) {
      $record->name = ucfirst($record->name);
    }
    
    $tags = get_tags(array('sort' => 'most'), 50);
    
    array_map('ucTags', $tags);
    sort($tags);
    echo tag_cloud($tags, uri('items/browse'));
    ?>

    The function ucTags does the job of uppercasing the first letter of the tag.
    array_map goes through the array of tags and applies that to each tag.

    I'm pretty sure that the links will still work, but haven't tested much.

    Hope that helps

  3. Thanks that works. Now everything is capitalized and in alphabetical order. However, it looks like you can keep both upper and lower case and have it alphabetized, since that's how it is on the Omeka forums page's popular tag list. How did you all achieve that?

  4. Instead of sort, you want to use a function that does case-insensitive sorting.

    I recommend natcasesort: it will sort its input case-insensitively, and it will sort in "natural" order, so "2" will sort before "10".

  5. That did the trick. Awesome, thanks.

Reply

You must log in to post.