简体   繁体   中英

How To? PHP echo data from within <script>

I'm at best a VERY novice programmer (hobbiest at best), i'm stuck on a piece of code i'm working on to implement Schema markup in to my Wordpress site.

Here's what i'm trying to solve.

 <script type='application/ld+json'> { "@context": "http://www.schema.org", "@type": "Festival", "name": "<?php the_title(); ?>", "url": "http://www.trancemusicevents.com/fbevent/solarstone-presents-pure-trance-helsinki-2017/", "description": " <?php the_excerpt(); ?>", "location": { "@type": "Place", "name": "The Circus Salomonkatu 13 Helsinki", "address": { "@type": "PostalAddress", "streetAddress": "<?php fbe_event_address(); ?>", "addressLocality": "Helsinki", "addressCountry": "Finland" } } } </script>

So where i have , and i'm trying to call custom post meta from Wordpress.

I've tried searching how to use php within the tags but found no joy. I basically just want to echo the meta data from the relevant Wordpress meta fields.

Any help is welcome, please excuse my novice approach to coding!

You need to escape the output of the_title for JOSN:

"name": "<?php echo esc_js(get_the_title()); ?>",

You can echo other custom fields the same way:

"field": "<?php echo esc_js(get_post_meta(get_the_ID(), 'my_custom_field_id', true)); ?>",

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM