简体   繁体   English

如何? PHP 从内部回显数据<script>

[英]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.我充其量只是一个非常新手的程序员(充其量是业余爱好者),我被困在一段代码上,我正在努力在我的 Wordpress 站点中实现 Schema 标记。

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.所以我在哪里,我正在尝试从 Wordpress 调用自定义帖子元。

I've tried searching how to use php within the tags but found no joy.我尝试搜索如何在标签中使用 php,但没有找到任何乐趣。 I basically just want to echo the meta data from the relevant Wordpress meta fields.我基本上只是想从相关的 Wordpress 元字段中回显元数据。

Any help is welcome, please excuse my novice approach to coding!欢迎任何帮助,请原谅我的新手编码方法!

You need to escape the output of the_title for JOSN:您需要为 JOSN 转义 the_title 的输出:

"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)); ?>",

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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