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.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.