Home › Forums › General Issues › get_field / get_sub_field conditional? › Reply To: get_field / get_sub_field conditional?
An old post, back to life!
My solution for this is a function.
function get_acf_field( $name, $id = null ) {
$field = get_sub_field( $name, $id ) ? get_sub_field( $name, $id ) : get_field( $name, $id );
return $field;
}
Seems to work for my needs. Hopefully it’ll help others. This gives me the ability to use the same component within a repeater/flex content or standalone on a page/post. @hube2 Your idea would also be a good solution so that the component fields are always a sub field. That makes sense too. Probably a bit faster too than doing the conditional check for every field.
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.