In WordPress I’m familiar with get_tags() and how to get all tags with:

<?php
$site_tags = get_tags();
foreach ($site_tags as $site_tag) : 
    if (strtolower($site_tag->name) == 'foobar') :
        continue;
    else : ?>
        <div class="tag_char"><?php echo $site_tag->name; ?></div>
    <?php endif; ?>
<?php endforeach; ?>

but I’m trying to figure out how to get all tags grouped by the first character, example:

A
_____
apple, alligator, alarm


B
_____
bug, boy, beer


C
_____
color, crayons, cheese

but when I try:

<?php
$site_tags = get_tags(array('order' => 'ASC'));
foreach ($site_tags as $site_tag) :
    $tag_first_char = substr($site_tag->name, 0, 1);
    ?>
    <div class="tag_char"><?php echo strtoupper($tag_first_char); ?></div>
    <?php
    foreach ($site_tags as $site_tag) :
        if ($tag_first_char == substr($site_tag->name, 0, 1)) :
        ?>
            <a href="<?php get_tag_link( $site_tag->term_id ); ?>" title="<?php $site_tag->name; ?>">
                <span class="the_tag"><?php $site_tag->name; ?></span>
            </a>
    <?php endif; ?>
<?php endforeach; ?>

It doesn’t render correctly. When researching the closet question I was able to find was:

What am I doing wrong and would it be better to use something like get_term_by() and pass it the first character so I’m not continually looping through the tags array causing possible performance issues?

Read more here: How can I get all tags with the same first character?


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: