Home > Enterprise >  Loop through custom WP taxonomy, grab ACF field value for each
Loop through custom WP taxonomy, grab ACF field value for each

Time:09-23

I feel like I'm really close to a solution here, I'm just not familiar enough with php or wordpress syntax to see where I'm going wrong.

In the footer (used on every page on the site), I'd like to loop through a custom taxonomy I've created. Each taxonomy item has an acf color picker field. Each taxonomy item will be displayed on the site sin its own div with a border. I'd like to use the color value as the border color for each of the corresponding taxonomy divs.

the taxonomy: topic

the color picker field: topic-color

Here's where I'm at so far:

$topic = get_terms( 'topic', array('hide_empty' => true) );
$acftopic = get_field('topic');
foreach($topic as $topic) {
  $color = get_field('topic-color', $acftopic->taxonomy . '_' . $term->term_id);
  echo '<a  style="border-color:' . $color . ';"href="' . get_category_link($topic->term_id) . '">' . $topic->name . '</a>';
  }
?>

This gives me the divs with the taxonomy items in each, but the border color is just black for all of them: there's no value where the color should be. This is what each individual div looks like:

<a  style="border-color:;" href="http://detroitpeer.local/topic/literacy/">Literacy</a>

CodePudding user response:

Try this:

$terms = get_terms([
    'taxonomy'   => 'topic', //your taxonomy name
    'hide_empty' => false,
]);
$acftopic = get_field('topic');

foreach($terms as $topic) {
  $color = get_field('topic-color', 'topic' . '_' . $topic->term_id);
  echo '<a  style="border-color:' . $color . ';"href="' . get_category_link($topic->term_id) . '">' . $topic->name . '</a>';
}
  • Related