WordPress.org

Ready to get started?Download WordPress

Forums

Alphabetical list custom tags -> even if empty (1 post)

  1. Plukker
    Member
    Posted 1 year ago #

    Hello there

    I created some code which returns an alphabetical list/index of custom tags and filters the custom posts in a grid layout. The tags are named as "tagdirectory". The custom post is named "directory".

    This is the code:

    <?php
    $list = '';
    $tags = get_terms( 'tagdirectory' );
    echo '<ul id="portfolio-filter">';
    echo '<li><a href="#all" title="">All</a></li>';
    $groups = array();
    if( $tags && is_array( $tags ) ) {
    foreach( $tags as $tag ) {
    $first_letter = strtoupper( $tag->name[0] );
    $groups[ $first_letter ][] = $tag;
    }
    if( !empty( $groups ) ) {
    foreach( $groups as $letter => $tags ) {
    $list .= "\n\t" . '<h2>' . apply_filters( 'the_title', $letter ) .'</h2>';
    $list .= "\n\t" . '<ul>';
    foreach( $tags as $tag ) {
    $lower = strtolower($tag->name);
    $name = str_replace(' ', ' ', $tag->name);
    $naam = str_replace(' ', '-', $lower);
    $list .= "\n\t\t" . '<li><a href="#'.$naam.'">'.$name.'</a></li>';
    }
    }
    }
    }else $list .= "\n\t" . '<p>Sorry, but no tags were found</p>';print $list;
    echo "</ul>";
    ?>

    This works perfectly but I would also like for empty letters from the alphabet to be shown.

    For example, now it returns:

    A
    Aicher Otl
    Apeloig Philippe
    B
    Bass Saul
    F
    Fitszimmons Maureen

    ... and so on

    But it doesn't show the empty letter groups because there are no tags starting with this letter. I do need it to show the capital letter for empty groups though, like so:

    A
    Aicher Otl
    Apeloig Philippe
    B
    Bass Saul
    C
    D
    E
    F
    Fitszimmons Maureen
    G
    ... and so on

    Can anybody help me and tell me what code I should add for this to work?

    Thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic