Support

Account

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' );