Home › Forums › General Issues › Displaying ACF field in page head (JSON script) › Reply To: Displaying ACF field in page head (JSON script)
If you are actually entering json code into the field then json_encode()
is not necessary.
When getting fields outside of “The Loop” you must supply the post ID to get the field from
function acf_header_script() {
$queried_object = get_queried_object();
if (is_a($queried_object, 'WP_Post')) {
$schema = get_field('schema_code', $queried_object->ID);
echo '<script type=application/ld+json>' . $schema_code . '</script>';
}
}
add_action ( 'wp_head', 'acf_header_script' );
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.