Woocommerce产品动态架构标记

时间:2019-03-19 14:03:21

标签: php html wordpress woocommerce schema

我有下一个代码可以动态添加WooCommerce产品的架构标记:

add_action('wp_head', 'schema_product');
function schema_product(){
global $product;
if (is_singular('product')) {  ?>
    <script type="application/ld+json">
    {
      "@context": "http://schema.org",
      "@type": "Product",
      "name": "<?php echo $product->get_name(); ?>",
      "description": "Ver descripción en el link incluido.",
      "image": "<?php echo get_the_post_thumbnail_url( $product->get_id(), 'full' ); ?>",
      "url": "<?php echo get_permalink( $product->get_id() ); ?>",
      "sku": "<?php echo $product->get_sku(); ?>",
      "brand": "<?php echo get_post_meta(get_the_ID(), 'brand', TRUE); ?>",
      "offers": {
        "@type": "Offer",
        "availability": "http://schema.org/<?php echo $product->is_in_stock() ? 'InStock' : 'OutOfStock'; ?>",
        "price": "<?php echo $product->get_price(); ?>",
        "priceValidUntil": "2019-12-31",
        "priceCurrency": "<?php echo get_woocommerce_currency(); ?>",
        "url": "<?php echo get_permalink( $product->get_id() ); ?>"
        },
      "aggregateRating": {
        "@type": "AggregateRating",
        "bestRating": "5",
        "ratingValue": "5",
        "reviewCount": "3"
        },
      "review": {
          "author": "Federico",
          "reviewRating": {
            "@type": "Rating",
            "bestRating": "5",
            "ratingValue": "5",
            "worstRating": "4"
          }
        }
    }
    </script>
<?php  }
};

但是当代码运行时,结果仅仅是

  

pe

在其他模式类型中,例如首页或博客文章,一切运行正常。但是似乎PHP在一处与HTML对抗。

1 个答案:

答案 0 :(得分:0)

您的代码中有一些错误……请尝试以下操作:

add_action('wp_head', 'schema_product');
function schema_product(){
    global $product;

    if ( is_product() && ! is_a($product, 'WC_Product') ) {
        $product = wc_get_product( get_the_id() );
    }

    if ( is_product() && is_a($product, 'WC_Product') ) :

    ?>
    <script type="application/ld+json">
    {
      "@context": "http://schema.org",
      "@type": "Product",
      "name": "<?php echo $product->get_name(); ?>",
      "description": "Ver descripción en el link incluido.",
      "image": "<?php echo get_the_post_thumbnail_url( $product->get_id(), 'full' ); ?>",
      "url": "<?php echo get_permalink( $product->get_id() ); ?>",
      "sku": "<?php echo $product->get_sku(); ?>",
      "brand": "<?php echo $product->get_meta('brand'); ?>",
      "offers": {
        "@type": "Offer",
        "availability": "http://schema.org/<?php echo $product->is_in_stock() ? 'InStock' : 'OutOfStock'; ?>",
        "price": "<?php echo $product->get_price(); ?>",
        "priceValidUntil": "2019-12-31",
        "priceCurrency": "<?php echo get_woocommerce_currency(); ?>",
        "url": "<?php echo $product->get_permalink(); ?>"
        },
      "aggregateRating": {
        "@type": "AggregateRating",
        "bestRating": "5",
        "ratingValue": "5",
        "reviewCount": "3"
        },
      "review": {
          "author": "Federico",
          "reviewRating": {
            "@type": "Rating",
            "bestRating": "5",
            "ratingValue": "5",
            "worstRating": "4"
          }
        }
    }
    </script>
    <?php
    endif;
}

代码在活动子主题(或主题)的function.php文件中。经过测试,可以正常工作。

enter image description here


对于“品牌”:

如果这是自定义分类法(例如使用Woocommerce Brands插件的wen),则您将需要替换:

"brand": "<?php echo $product->get_meta('brand'); ?>",

通过此行(此处的分类法为'product_brand')

"brand": "<?php $brand = wp_get_post_terms( $product->get_id(), 'product_brand' ); $brand = reset($brand); echo $brand->name; ?>",