Support

Account

Home Forums ACF PRO How do you setup a Category Taxonomy in a function within Functions.php ?

Solved

How do you setup a Category Taxonomy in a function within Functions.php ?

  • I have a image field added to the Posts Category taxonomy and it’s working great within the category.php template page.

    However I do have a function called on other pages which I’d like to reference the same image field.

    I can’t seem to pull the field due to requiring additional details to call from another page but I can’t seem to get it working.

    I’ve seen many suggest

    the_field('image', 'category_' . $post->ID);

    or

    
    $queried_object = get_queried_object(); 
    $taxonomy = $queried_object->taxonomy;
    $term_id = $queried_object->term_id;  
    
    the_field('image', $post->ID);

    Sadly neither of these are working from the functions.php function.

    The end goal would be to have an if statement checking if that field exists and returning a response if it does.

    function imageFound($atts){
    if(get_field('image', $post->ID))
     { 
       the_field('image', $post->ID);
       } else { 
       echo 'Sorry, the image:'. $atts . ' was not found.';
     }
    }

    I’d make more use of $atts but I’m just simplifying it for this example.

    I’m appreciative of any input regarding this.

  • Hi @matt-seligman

    You need to pass the ID of the category where the image is located. If you add the image to a category with the ID of 1, for example, you need to use this code:

    $cat_id = '1';
    the_field('image', 'category_' . $cat_id);

    Or:

    the_field('image', 'category_1');

    This page should give you more idea about it: http://www.advancedcustomfields.com/resources/get-values-from-a-taxonomy-term/.

    I hope this helps.

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘How do you setup a Category Taxonomy in a function within Functions.php ?’ is closed to new replies.