phpwordpresstaxonomy

Trying to get the name of a term in custom taxonomy called 'pwb-brand' by ID


I've been on this for a while now, trying to find something similar. And I bet there is - I just can't seem to find it.

Basically what I am trying to do is getting the name of a term located in a custom taxonomy by ID.

The taxonomy is called pwb-brand

The term ID is generated from an Advanced Custom Field the_sub_field('varumarke')

All I get in return is the ID of the term but I don't get the name as wished for.

<?php $brands = get_term_by('id', the_sub_field('varumarke'), 'pwb-brand'); ?>
<?php foreach( $brands as $brand ):
    echo '<h2>' . $brand->name . '</h2>';
endforeach; ?>

Solution

  • Rule-of-thumb with WordPress: Functions named the_something normally write to the output buffer directly, whereas their get_something counterparts return the value.

    get_term_by('id', the_sub_field('varumarke'), 'pwb-brand');
    

    You are effectively not passing any value for the second parameter here - because the_sub_field echos the value, and doesn't return anything.

    You need to use get_sub_field here instead.