0

Just wanted to get an opinion, or ideas. I have an CPT called "Listings", and a custom taxonomy for it called directory-category.

I have put together the below function to generate a list of links for the terms for the custom taxonomy.

I tried using get_term_link() and couldn't get it to generate the links properly.

This code is working and when called it generates the links to the archive of each tax term.
However, I am thinking there is a better way to accomplish this, any insight would be appreciated, thanks!

function ebx_listing_tax_links() {
    echo '<div class = "ebx-cat-bar">';
    $url = site_url();
    $cat = "/directory-category/";
    $terms = get_terms( 'listing_categories' );
    if($terms) : foreach ( $terms as $term ) {
        ?><a class="ebx-cat-link" href="<?php
        echo $url;
        echo $cat;
        echo $term->slug;
        ?>"><?php echo $term->name; ?></a><?php
    } endif;
    echo '</div>';
}
  • I wouldn't use that code - it may break a lot of things... Main question is: why get_term_link isn't working correctly? What's wrong with it? How do you use it? How is the taxonomy registered? – Krzysiek Dróżdż Aug 23 '18 at 8:35
  • I agree, looked at it again this morning and changed it back to the get_term_link , I had a concatenation mistake with the syntax. – colbyalbo Aug 23 '18 at 12:20
0

Got this to work, i was concatenating the html incorrectly.

$terms = get_terms( 'listing_categories' ); if($terms) : foreach ( $terms as $term ) { echo '<li><a href="'.get_term_link($term).'">'.$term->name.'</a></li>'; } endif;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.