Home Forums Front-end Issues Adding a classname to the body class Reply To: Adding a classname to the body class

  • In the function ACF is assuming that the post you want to get the value from is the current post. In the case of a term it is probably getting the first post in the list.

    The first thing that you need to do is to figure out what is actually being queried and then do something different and/or set the correct post ID.

    // see what WP is actually showing.
    $queried_object = get_queried_object();
    if (is_a($queried_object, 'WP_Post')) {
      // queried object is a post
      $post_id = $queried_object->ID;
    } elseif (is_a($queried_object, 'WP_Term')) {
      // queried object is a term
      $post_id = 'term_'$queried_object->term_id;
    // and an acf field
    $value = get_field('some_field_name', $post_id);

    You can also do other things based on what type of object is being shown.