首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Blogposting &没有插件的结构化数据

Blogposting &没有插件的结构化数据
EN

WordPress Development用户
提问于 2019-04-17 20:39:24
回答 1查看 1.3K关注 0票数 0

我想添加blogposting模式标记,以便为丰富的数据自动优化每个新帖子。网络上的一些代码是杂乱无章的,每个帖子都需要更新,而且插件也不是最好的选择,因为这是自定义主题,我们正试图将插件需求降到最低。是否有一个代码,我可以添加到single.php文件,以便每个页面都有适当的结构化数据,没有插件,或在管理或每个帖子额外的变化。谢谢!

EN

回答 1

WordPress Development用户

发布于 2019-04-17 21:23:21

我认为你想去的路线是用JSON-LD。

来自CSS的这是一个很好的指南来指导你如何去做

概述:

  1. 确定您的帖子需要的模式元素
  2. 使用functions.php (或者包含一个.php文件,但是您可以组织站点的附加功能,我通常会创建自己的.php文件)。
  3. 使用站点中的变量和数组中的帖子定义json值(指南的步骤5)。最棒的是所有的ACF和wordpress都是这样提供给你的。你可以称之为节选,内容,标题等。
  4. 在你的帖子页面头上运行你的json脚本。

指南中的例子:

代码语言:javascript
复制
add_action('wp_head', function() {
  $schema = array(
    // Tell search engines that this is structured data
    '@context'  => "http://schema.org",
    // Tell search engines the content type it is looking at 
    '@type'     => get_field('schema_type', 'options'),
    // Provide search engines with the site name and address 
    'name'      => get_bloginfo('name'),
    'url'       => get_home_url(),
    // Provide the company address
    'telephone' => '+49' . get_field('company_phone', 'options'), //needs country code
    'address'   => array(
      '@type'           => 'PostalAddress',
      'streetAddress'   => get_field('address_street', 'option'),
      'postalCode'      => get_field('address_postal', 'option'),
      'addressLocality' => get_field('address_locality', 'option'),
      'addressRegion'   => get_field('address_region', 'option'),
      'addressCountry'  => get_field('address_country', 'option')
    )
  );
}

指南中的例子:

代码语言:javascript
复制
echo '' . json_encode($schema) . '';

这是我找到的最好的方法,因为您可以在模板中使用wordpress中的所有可用内容,包括ACF,并且可以添加条件在特定页面上运行脚本。

票数 0
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/334635

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档