How to pass an advanced custom field from php template into a script in footer

Yup, that will work. When it is done that way, the script tag can end up in rather odd position on the page. Plus modifying templates is not always optimal. FWIW, there is a more organized, formal way to do this. See
https://developer.wordpress.org/reference/functions/wp_localize_script/

Ohh nice thank you!! oj I used this

<script type="application/ld+json">
     var recipeName = '<?php echo the_field('recipe_name'); ?>';
</script>

and not this, sorry I realize I didn’t specify which method I used…

<script type="application/ld+json">
     {
      "@context": "http://schema.org/",
      "@type": "Recipe",
      "name": "<?php the_field('recipe_name'); ?>",
      "image": "http://images.media-allrecipes.com/userphotos/600x600/1116471.jpg",
      "author": {
      "@type": "Person",
      "name": "scoopnana"
     }
     }
</script>

If I got it correctly the better way should be applied to the second code

  • This reply was modified 1 day, 12 hours ago by snippet24.
  • This reply was modified 1 day, 12 hours ago by snippet24.



Source link