timkeay
timkeay

Reputation: 104

WordPress get_terms not outputting URL

I've got the following PHP code that I'm using to output a list of all custom taxonomy values, then group them into alphabetical order by first letter. This is working fine, except that the URL isn't being outputted. Anyone able to help?

<?php
$list = '';
$tags = get_terms( 'film-categories' );
$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 .= '<div class="cat-group"><h3>' . apply_filters( 'the_title', $letter ) . '</h3>';
            $list .= '<ul>';
            foreach( $tags as $tag ) {
                $url = esc_attr( get_tag_link( $tag->term_id ) );
                $name = apply_filters( 'the_title', $tag->name );
                $list .= '<li><a href="' . $url . '">' . $name . '</a></li>';
            }
            $list .= '</ul></div>';
        }
    }
} else $list .= '<p>Sorry, but no tags were found</p>';

echo $list;

?>

Upvotes: 0

Views: 830

Answers (1)

Ofir Baruch
Ofir Baruch

Reputation: 10346

I'm afraid you've confused. According to your second line - you're fetching terms of custom tax and not tags. $tags = get_terms( 'film-categories' );

Therefore , any function related to tags won't work correctly. In order to get the url of the term use the get_term_link() function.

Just replace the current line with:

$url = esc_attr( get_term_link( $tag ) );

Should work.

Upvotes: 1

Related Questions