1

Using the 'Advanced custom fields' plugin I have added a field to every post category that is called 'color' and it works just fine.

Now inside the loop I need to retrieve the value of the 'color' field, I have followed many other tutorials, but I could not achieve the result needed.

Here is the exaple of how the loop is set up:

    while ( $projects->have_posts() ) {
        $projects->the_post();

        $category_classes = array();
        $categories = get_the_terms( get_the_ID(), 'project_category' );
        if ( $categories ) {
            foreach ( $categories as $category ) {
                $categories_included[] = $category->term_id;
            }
        }

Now in perfect scenario, I need to get the 'color' of a category inside the foreach loop.

Any help or guidance is much appreciated.

1 Answer 1

2

http://www.advancedcustomfields.com/resources/get-values-from-a-taxonomy-term/ prety much covers what you're after I believe.

So in your example

while ( $projects->have_posts() ) {
    $projects->the_post();

    $category_classes = array();
    $categories = get_the_terms( get_the_ID(), 'project_category' );
    if ( $categories ) {
        foreach ( $categories as $category ) {
            $categories_included[] = $category->term_id;
            $color = get_field('color', 'project_category_'.$category->term_id);
        }
    }

You can also pass a term to get_field and the_field, so you could also use the following depending on your version of ACF as I believe much older versions don't support passing terms

$color = get_field('color', $category);
1
  • Thanks a lot, sencond solution with simply passing field name and category works perfect.
    – WpDoe
    Commented Dec 15, 2015 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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