无需插件的丰富代码片段编码 Wordpress

Rich Snippet coding Wordpress without plugin

我希望向我的 Wordpress 网站添加一些丰富网页摘要。我在哪里可以找到关于这个主题的正确文档?我需要在没有现有 Wordpress 插件的情况下实施 Rich Snippet。如有需要,我可以自己开发。

我确实知道 PHP、HTML 和 CSS。

搜索引擎通过选取您本应在主题中实施的微数据架构来生成丰富网页摘要。

您需要编辑您的主题,并实现您想要实现的任何架构。例如,如果它是一种产品。您需要遵循此架构 https://schema.org/Product

    <div itemscope itemtype="http://schema.org/Product">
  <img itemprop="image" src="dell-30in-lcd.jpg" alt="A Dell UltraSharp monitor"/>
  <span itemprop="name">Dell UltraSharp 30" LCD Monitor</span>
  <div itemprop="aggregateRating"
    itemscope itemtype="http://schema.org/AggregateRating">
    <span itemprop="ratingValue">87</span>
    out of <span itemprop="bestRating">100</span>
    based on <span itemprop="ratingCount">24</span> user ratings
  </div>
  <div itemprop="offers" itemscope itemtype="http://schema.org/AggregateOffer">
    <span itemprop="lowPrice">50</span>
    to <span itemprop="highPrice">95</span>
    from <span itemprop="offerCount">8</span> sellers
    Sellers:
    <div itemprop="offers" itemscope itemtype="http://schema.org/Offer">
        <a itemprop="url" href="save-a-lot-monitors.com/dell-30.html">
        Save A Lot Monitors - 50</a>
    </div>
    <div itemprop="offers" itemscope itemtype="http://schema.org/Offer">
        <a itemprop="url" href="jondoe-gadgets.com/dell-30.html">
        Jon Doe's Gadgets - 50</a>
    </div>
  </div>
  ...
</div>

使用 html 中的架构模板,您可以将其合并到您的主题中,并使用动态内容填充您的页面。

如果以上信息对您没有帮助,请询问有关丰富网页摘要或特定架构的具体问题。

在您对 Company/Organization

发表评论后添加

为此,我推荐相同的方法,但遵循 https://schema.org/Organization

的架构
    <div itemscope itemtype="http://schema.org/Organization">
  <span itemprop="name">Google.org (GOOG)</span>
Contact Details:
  <div itemprop="address" itemscope itemtype="http://schema.org/PostalAddress">
    Main address:
      <span itemprop="streetAddress">38 avenue de l'Opera</span>
      <span itemprop="postalCode">F-75002</span>
      <span itemprop="addressLocality">Paris, France</span>
    ,
  </div>
    Tel:<span itemprop="telephone">( 33 1) 42 68 53 00 </span>,
    Fax:<span itemprop="faxNumber">( 33 1) 42 68 53 01 </span>,
    E-mail: <span itemprop="email">secretariat(at)google.org</span>
Members:
- National Scientific Members in 100 countries and territories: Country1, Country2, ...
- Scientific Union Members, 30 organizations listed in this Yearbook:
  <span itemprop="member" itemscope itemtype="http://schema.org/Organization">
    Member1
  </span>,
  <span itemprop="member" itemscope itemtype="http://schema.org/Organization">
    Member2
  </span>,
History:
</div>

我正在寻找一种将结构化数据添加到我的网站的方法,并遇到了这个话题。 Google 更喜欢 JSON-LD 所以不太符合我的要求。我最终想出了如何使用 JSON.

添加它

函数文件:

function schema_mark_up () {
 while ( have_posts() ) : the_post();
            $post_id = get_the_ID();
            
            $name = get_the_title();
            $permalink = get_permalink();
            $images = array (get_post_meta($post_id, 'banner_picture', true),
                            get_post_meta($post_id, 'profile_picture', true)
                            );
            $street_address = array (
                                get_post_meta($post_id, 'address', true),
                                get_post_meta($post_id, 'address_1', true)              
                            );  
            $address_locality = get_post_meta($post_id, 'Suburb', true);
            $address_locality = get_post_meta($post_id, 'Province', true);
            $postal_code = get_post_meta($post_id, 'Postal Code', true);
            $address_country = get_post_meta($post_id, 'Country', true);
            $telephone = get_post_meta($post_id, 'Contact Number', true);
            
            $data = array (
                    '@context' => 'http://www.schema.org',
                    '@type' => 'LocalBusiness',
                    'image' => $images,
                    '@id' => $permalink,
                    'name' => $name,
                    'address' => array (
                        '@type' => 'PostalAddress',
                        'streetAddress' => $street_address,
                        'addressLocality' => $address_locality,
                        'addressRegion' => $address_region,
                        'postalCode' => $postal_code,
                        'addressCountry' => $address_country
                    ),
                    'priceRange' => 'ZAR',
                    'telephone' => $telephone
            );
            
            $json = json_encode($data);
            echo $json;
    endwhile;
}

在您的 header 或您想调用代码的任何地方:

<script type="application/ld+json">
    <?php schema_markup (); ?>
</script>