Support

Account

Home Forums General Issues Display category acf fields on category/archive header

Solved

Display category acf fields on category/archive header

  • What is the best way to display fields set for taxonomy = category in a category header? I can’t seem to make any of the php code samples work with get_field(‘fieldname’) for any of the fields.

    My goal is to have set of social media and other link type icons at the bottom of the category header like this image, with the urls for each icon generated by the values I enter for the fields named facebook, twitter, website, etc.

    …but any ways I’ve tried to add code for these fields to the header section come up blank. I am able to edit the header in other ways, like how I added the icons.

    Maybe I need to add something to “get_field” so something knows its related to a category and not a post? Thanks.

  • Yes, you’re correct, you need to add something. See “Get a value from different objects” https://www.advancedcustomfields.com/resources/get_field/

    
    $post_id = 0;
    $queried_object = get_queried_object();
    if (isset($queried_object->term_id)) {
      $post_id = $queried_object->taxonomy.'_'.$queried_object->term_id;
    }
    
    // ....
    
    $value = get_field('my-field', $post_id);
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.