无法将Yoast元描述打印到页面模板中(语法错误,意外的‘.’)

时间:2021-05-13 作者:Drewdavid

以下是我的页面模板代码的相关部分:

$yoast_meta = get_post_meta($post->ID, \'_yoast_wpseo_metadesc\', true);

function my_service_template_footer_data() {
    echo "<script type=\'application/ld+json\'>
{
    \'@context\': \'https://schema.org/\',
    \'@type\': \'Service\',
    \'serviceType\': \' " . get_the_title() . " \',
    \'description\': \' " . $yoast_meta; . " \',
    \'provider\': {
      \'@type\': \'LocalBusiness\',
      \'name\': \'...\',
  \'address\': {
      \'@type\': \'PostalAddress\',
      \'addressLocality\': \'...\',
      \'addressRegion\': \'...\',
      \'postalCode\': \'...\',
      \'streetAddress\': \'...\'
    },
    \'providerMobility\':\'...\',
    \'telephone\': \'...\',
    \'image\': \'...\',
    \'PriceRange\': \'...\',
    \'serviceArea\': [{
        \'@type\': \'...\',
      \'name\': \'...\',
      \'@id\': \'...\'
      }]
  }
}
</script>";
        
}
保存文件时,我从WP\\u DEBUG得到以下错误:

Parse error: syntax error, unexpected \'.\' in /home/260255.cloudwaysapps.com/hyubphmutk/public_html/wp-content/themes/twentytwentyone-child/functions.php on line 99 There has been a critical error on this website.

如何重复使用Yoast元描述?谢谢

1 个回复
最合适的回答,由SO网友:Sally CJ 整理而成

确实有一个不想要的; (分号)(请尝试查看twentytwentyone-child/functions.php 文件),这可能是语法错误的原因。所以把它去掉; 错误就会消失:

function my_service_template_footer_data() {
    echo "<script type=\'application/ld+json\'>
{
    \'@context\': \'https://schema.org/\',
    \'@type\': \'Service\',
    \'serviceType\': \' " . get_the_title() . " \',
    \'description\': \' " . $yoast_meta; . " \', // remove the ;
    ...
但该变量实际上没有定义,或者在my_service_template_footer_data(), 因此,您应该将变量移动/复制到该函数中,并使用get_the_ID() 而不是$post->ID, 像这样:

function my_service_template_footer_data() {
    $yoast_meta = get_post_meta( get_the_ID(), \'_yoast_wpseo_metadesc\', true );
    ...
但是,您的JSON数据的格式无效,例如属性名称应该用双引号括起来,因此您可以使用"description" 而不是\'description\'. 此外,必须正确转义属性值,这可以通过wp_json_encode() 函数(本机的增强版本json_encode() PHP中的函数)。

话虽如此,请尝试使用?>HTML here<?php 语法和非echo \'HTML here\':

Update: 上述JSON编码函数将返回值括在双引号中,因此不要这样做"<?php echo wp_json_encode( ... ); ?>", i、 e.不包括引号(").

function my_service_template_footer_data() {
    $yoast_meta = get_post_meta( get_the_ID(), \'_yoast_wpseo_metadesc\', true );
    ?>
<script type="application/ld+json">
{
    "@context": "https://schema.org/",
    "@type": "Service",
    "serviceType": <?php echo wp_json_encode( get_the_title() ); ?>,
    "description": <?php echo wp_json_encode( $yoast_meta ); ?>,
    "provider": {
        "@type": "LocalBusiness",
        "name": "...",
        "address": {
            "@type": "PostalAddress",
            "addressLocality": "...",
            "addressRegion": "...",
            "postalCode": "...",
            "streetAddress": "..."
        },
        "providerMobility":"...",
        "telephone": "...",
        "image": "...",
        "PriceRange": "...",
        "serviceArea": [{
            "@type": "...",
            "name": "...",
            "@id": "..."
        }]
    }
}
</script>
    <?php
}
PS:如果get_the_ID() 没有为您提供正确的ID,请尝试使用get_queried_object_id() 相反,然后使用get_the_title( get_queried_object() ). (您可以验证结构化数据here.. :) )