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; ?>
CodePudding user response:
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.
CodePudding user response:
You're assigning $brands to the value returned from a single term with get_term_by
- if you want an array of brands to loop over you need to get multiple terms with something like:
$brands = get_terms( array(
'taxonomy' => 'pwb-brand',
));
Then you loop over each $brand in $brands to get the name. Is the value from the_sub_field
actually an ID? You should always dump out your variables at each stage to check that the value is what you expect and work backwards from there.
echo '<pre>';
var_dump( $brands );
echo '</pre>';
die();