Sort tags on different divs

for the development of our site, we need sort the views of tags alphabetically and separated in divs by initial letter, example:




We have not solved the split in divs, can you help us?

Thank you very much

So, you’ve got them printing in order but not separated?

Assuming you’re just looping through an array, producing a div around each different first letter isn’t hard if you’re starting with a sorted list: you just loop through as normal and look for where the first letter changes.

// Your array of tags is $tags, assuming an array of strings
$initial = current($tags)[0];
$initialTags = array();
echo '<div>';
echo '<h2>' . html_escape($initial) . '</h2>';
foreach ($tags as $tag) {
    if ($initial != $tag[0]) {
        $initial = $tag[0];
        echo '</div><div>';
        echo '<h2>' . html_escape($initial) . '</h2>';
    echo '<p>' . html_escape($tag) . '</p>';
echo '</div>';

Sorry for my low level of knowledge, but where do I have to put this code?

Thanks again

I’m understanding this as wanting to change what’s displayed on the “Browse by tags” page. You’d first want to override the theme’s default page, which probably produces a tag cloud.

There’s a couple things that need to happen. First, is sorting the tags alphabetically, then grouping them, then displaying them with the divs you want. The sorting could be handled more efficiently with a plugin, but here’s some code for items/tags.php that I think will do the trick.

$pageTitle = __('Browse Items');
echo head(array('title' => $pageTitle, 'bodyclass' => 'items tags'));

function sortTags($a, $b) {
    return (strtolower($a['name']) < strtolower($b['name'])) ? -1 : 1;

usort($tags, 'sortTags');

$groupedTags = array();
foreach($tags as $tag) {
    $initial = strtolower( substr($tag['name'], 0, 1) );
    $groupedTags[$initial][] = $tag;


<h1><?php echo $pageTitle; ?></h1>

<nav class="navigation items-nav secondary-nav">
    <?php echo public_nav_items(); ?>

<?php foreach($groupedTags as $initial => $tags): ?>
<h2><?php echo strtoupper($initial); ?></h2>
    <?php foreach ($tags as $tag):
    $name = $tag['name'];
    <p><?php echo '<a href="' . html_escape(url('items/browse', array('tags' => $name))) . '" rel="tag">' . html_escape($name) . '</a>'?></p>
    <?php endforeach;?>
<?php endforeach;?>

<?php echo foot(); ?>

The sortTags() function sorts them alphabetically, then the $groupedTags nested array gives something grouped by letter. The foreach then produces the output, so the specific HTML you want can be added there.

Sorry for the delay in replying, I have tried your code and it works perfectly, thank you very much.

I hope the web can be available as soon as possible and share it with all of you, thanks again.