admin管理员组文章数量:1122846
I have in the heder.php the following snippet:
<?php
$schema = get_post_meta(get_the_ID(), 'schema', true);
if (!empty($schema)) {
echo $schema;
}
?>
And in some posts I have the custom field schema
with all the <script type="application/ld+json">{... content here
I had this few years and it was always working fine.
Since few days in Google Search Console I see errors Unparsable structured data -> Invalid top level element "null"
I see that the DOM renders the right <script type="application/ld+json">
code for logged in users.
But for non logged in users it renders <script type="application/ld+json">null</script>
Any idea what could have happened and how to resolve it? There are no errors in the console.
Here is an example of a page that has this issue.
Update: I've also tried the following:
$key_name = get_post_custom_values($key = 'schema');
if (!empty($key_name[0])) {
echo $key_name[0];
}
But there is the same problem. Works for logged in users, and does not work for logged of visitors.
I tried also: <?php echo get_post_meta($post->ID, 'schema', true); ?>
, but the same problem.
P.s. in the debug.log there are no errors. Just some deprecation notices. Also no console.log errors.
Update 2:
It seems I found something relevant.
In the custom field I have a similar code much longer)... but it seems that the null
appears if I add the description
:
<script type="application/ld+json">
{
"@context": ";,
"@type": "Event",
"name": "name name",
"image": "path to url",
"description": "some text some text - some text: some text",
}
</script>
I fi remove the description it works fine.
So I thought it may be related to the :
character.... and yes!
If I escape \:
it works!
It seems something changed in the parser in the Wordpress new versions.... because in the past it was working fine. That json was showing well for years... The same happens for new lines (enter) in strings.
本文标签: Custom fieldsgetpostmeta giving null
版权声明:本文标题:Custom fields - get_post_meta giving null 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736308591a1933710.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论