Duggy G Duggy G - 1 year ago 84
PHP Question

Advanced Custom Fields category link

I'm struggling with this even after looking at the documentation but all I'm wanting to do is link to a category. I have created a taxonomy field to select categories. Everything is working apart from when I add

<a href="<?php echo the_sub_field('category_link','option' ); ?>"></a>


I just get the ID of the category but I want the name. This is all within a repeater as I'm creating a menu but I need help getting the name and not the ID.

any help please?

Answer Source

If there is an option of selecting only one taxonomy.

Then write the query:

    <?php $term_id = get_sub_field('category_link');
    if( $term_id):
        $term_name = get_cat_name( $term_id ) ;
        $term_url = get_category_link( $term_id ); ?>
        <a href="<?php echo term_url; ?>"><?php echo $term_name; ?></a>
   <?php endif; ?>

OR

If there are multiple category terms, then place this query:

     <?php 
    $terms = get_sub_field('category_link');
    if( $term_ids ):
        foreach( $term_ids as $term_id ): 
            $term_name = get_cat_name( $term_id ) ;
            $term_url = get_category_link( $term_id ); ?>
            <a href="<?php echo term_url; ?>"><?php echo $term_name; ?></a>
   <?php endforeach;  endif;    ?>

Now place your category term url & name in the anchor tag.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download