一套面向 WordPress 企业官网 / B2B 外贸网站 的轻量级 JSON-LD Schema 配置方案。
重点覆盖首页、博客详情页、产品详情页和普通 Page 页面,强调 真实、通用、可复用、易维护。
当前版本:v1.0.0
很多企业官网并不需要复杂的 Schema 插件,也不需要为了“字段更多”而堆叠大量结构化数据。
这个项目希望解决的是一个更实际的问题:
如何用一套简单、稳定、容易复用的代码,为 WordPress 企业官网建立基础 Schema 体系?
当前方案基于 WordPress 原生数据和 JSON-LD 输出,主要面向:
- 企业官网
- B2B 外贸网站
- 产品展示型网站
- 非 WooCommerce 产品站
- 自定义主题网站
- 多站点复用场景
核心思路:
WordPress 后台数据
↓
页面真实内容
↓
Schema Functions
↓
JSON-LD
↓
Google / Schema.org
| 页面类型 | Schema | 作用 |
|---|---|---|
| 首页 | Organization + WebSite |
明确品牌、官网名称、Logo 与网站实体关系 |
| 博客详情页 | BlogPosting |
描述文章标题、作者、日期、主图、分类、Tags |
| 产品详情页 | Product |
描述产品名称、图片、分类、品牌和产品页面 |
| 普通 Page | WebPage |
描述普通企业页面,并建立与 WebSite / Organization 的关系 |
最终结构:
Homepage
├── Organization
└── WebSite
Blog Post
└── BlogPosting
Product
└── Product
Page
└── WebPage
Schema 必须描述页面中真实存在的信息。
不为了让测试结果“更漂亮”而伪造:
price
availability
review
rating
aggregateRating
尽量保持:
WordPress 后台数据
↓
页面显示内容
↓
Schema
三者一致。
只输出真正有意义、稳定、可维护的字段。
不为了“Schema 更多”而增加:
articleBody
wordCount
commentCount
sameAs
FAQPage
代码不写死:
- 域名
- 网站名称
- 作者
- Logo
默认自动读取 WordPress 数据。
如果后续安装:
- Yoast SEO
- Rank Math
- AIOSEO
- WooCommerce SEO 插件
必须先检查它们是否已经输出同类型 Schema。
部署前建议确认:
- 当前网站没有其他插件重复输出相同 Schema
- WordPress
Site Title已正确设置 - WordPress
Custom Logo已设置 - 博客文章 Author 与页面显示作者一致
- 博客文章建议设置 Featured Image
- 产品 Post Type 与代码一致
- 产品分类法与代码一致
- 产品图片字段与代码一致
- 普通 Page 的后台标题与页面实际含义一致
Schema 中的 Organization.logo 默认读取 WordPress 原生 Custom Logo:
get_theme_mod( 'custom_logo' )因此在部署 Schema 前,建议先确认主题是否已经启用并设置 Custom Logo。
进入 WordPress 后台:
外观(Appearance)
→ 自定义(Customize)
→ 站点身份(Site Identity)
如果可以看到:
Logo
→ Select logo
说明当前主题已经支持 WordPress 原生 Custom Logo,可以直接进入下一步上传。
注意:
Site Icon不是Custom Logo。
Site Icon主要用于浏览器标签页、书签和站点小图标;本项目 Schema 中的publisher.logo/Organization.logo读取的是Custom Logo。
在当前主题的 functions.php 中加入:
function site_schema_theme_support() {
if ( ! current_theme_supports( 'custom-logo' ) ) {
add_theme_support( 'custom-logo' );
}
}
add_action( 'after_setup_theme', 'site_schema_theme_support', 20 );保存后重新进入:
外观
→ 自定义
→ 站点身份
正常情况下会新增:
Logo
→ Select logo
下方“推荐代码”已经包含这段 Theme Support。如果直接使用完整代码,不需要重复添加。
操作步骤:
WordPress 后台
→ 外观
→ 自定义
→ 站点身份
→ Logo
→ Select logo
→ 上传或从媒体库选择正式品牌 Logo
→ 如出现裁剪步骤,根据实际 Logo 选择裁剪或跳过
→ 发布(Publish)
建议使用:
- 正式品牌 Logo
- 清晰、可公开访问的图片
- 不带产品广告、Banner 或无关宣传文字
- 与网站真实品牌保持一致
发布后打开任意已经输出 Schema 的页面:
Ctrl + U
查看网页源代码,搜索:
application/ld+json
然后确认存在类似内容:
"publisher": {
"@type": "Organization",
"name": "Example",
"url": "https://www.example.com/",
"logo": {
"@type": "ImageObject",
"url": "https://www.example.com/wp-content/uploads/logo.png"
}
}首页的 Organization Schema 中也应出现对应的:
"logo": {
"@type": "ImageObject",
"url": "..."
}部分老的自定义主题会在 header.php 中直接写死 Logo 图片地址,而不是调用 WordPress 原生 Custom Logo。
这种情况下:
后台 Custom Logo
→ Schema 可以正常读取
但网站 Header 原来的 Logo:
不一定会自动替换
这不是 Schema 错误。
如果希望 Header 与 Schema 共用同一个 Logo 数据源,可以进一步让主题 Header 使用:
the_custom_logo();或通过:
get_theme_mod( 'custom_logo' );读取 WordPress 原生 Logo。
经典主题通常通过:
外观 → 自定义 → 站点身份
设置 Logo。
部分新主题可能通过站点编辑器提供 Logo 设置入口。无论入口如何变化,本项目的关键是:
Logo 最终应由 WordPress 原生 Custom Logo 机制保存并能够通过
get_theme_mod( 'custom_logo' )读取。
建议将下面代码放入当前主题的:
functions.php
正式项目中建议将 site_ 前缀替换为当前项目唯一前缀,例如:
coowin_
evodek_
company_
/**
* WordPress 通用 Schema 基础组件
*/
// 如果主题没有原生 Custom Logo 功能,则启用
function site_schema_theme_support() {
if ( ! current_theme_supports( 'custom-logo' ) ) {
add_theme_support( 'custom-logo' );
}
}
add_action( 'after_setup_theme', 'site_schema_theme_support', 20 );
// 获取站点 Organization 数据
function site_schema_organization() {
$home_url = home_url( '/' );
$organization = array(
'@type' => 'Organization',
'@id' => $home_url . '#organization',
'name' => get_bloginfo( 'name' ),
'url' => $home_url,
);
$custom_logo_id = get_theme_mod( 'custom_logo' );
if ( $custom_logo_id ) {
$logo_url = wp_get_attachment_image_url( $custom_logo_id, 'full' );
if ( $logo_url ) {
$organization['logo'] = array(
'@type' => 'ImageObject',
'url' => $logo_url,
);
}
}
return $organization;
}
// 输出 JSON-LD
function site_schema_output( $schema ) {
echo "\n<script type=\"application/ld+json\">";
echo wp_json_encode(
$schema,
JSON_UNESCAPED_UNICODE |
JSON_UNESCAPED_SLASHES |
JSON_HEX_TAG |
JSON_HEX_AMP |
JSON_HEX_APOS |
JSON_HEX_QUOT
);
echo "</script>\n";
}
/**
* 首页 Schema
* Organization + WebSite
*/
function site_homepage_schema() {
if ( ! is_front_page() ) return;
$home_url = home_url( '/' );
$organization = site_schema_organization();
$website = array(
'@type' => 'WebSite',
'@id' => $home_url . '#website',
'url' => $home_url,
'name' => get_bloginfo( 'name' ),
'publisher' => array(
'@id' => $home_url . '#organization',
),
);
site_schema_output(
array(
'@context' => 'https://schema.org',
'@graph' => array(
$organization,
$website,
),
)
);
}
add_action( 'wp_head', 'site_homepage_schema', 20 );
/**
* 博客详情页 Schema
* BlogPosting
*/
function site_blogposting_schema() {
if ( ! is_singular( 'post' ) ) return;
$post_id = get_queried_object_id();
$post = get_post( $post_id );
if ( ! $post ) return;
$url = get_permalink( $post_id );
$headline = wp_strip_all_tags( get_the_title( $post_id ) );
$date_published = get_the_date( DATE_W3C, $post_id );
$date_modified = get_the_modified_date( DATE_W3C, $post_id );
if ( empty( $date_modified ) ) {
$date_modified = $date_published;
}
$description = wp_strip_all_tags( get_the_excerpt( $post_id ) );
if ( empty( $description ) ) {
$description = wp_trim_words(
wp_strip_all_tags(
strip_shortcodes( $post->post_content )
),
35
);
}
$author_id = (int) $post->post_author;
$author_name = get_the_author_meta( 'display_name', $author_id );
$author_url = get_author_posts_url( $author_id );
$image_url = get_the_post_thumbnail_url( $post_id, 'full' );
$categories = get_the_category( $post_id );
$article_section = ! empty( $categories ) ? $categories[0]->name : '';
$tags = get_the_tags( $post_id );
$keywords = array();
if ( $tags ) {
foreach ( $tags as $tag ) {
$keywords[] = $tag->name;
}
}
$schema = array(
'@context' => 'https://schema.org',
'@type' => 'BlogPosting',
'@id' => $url . '#blogposting',
'mainEntityOfPage' => array(
'@type' => 'WebPage',
'@id' => $url,
),
'headline' => $headline,
'description' => $description,
'url' => $url,
'datePublished' => $date_published,
'dateModified' => $date_modified,
'author' => array(
'@type' => 'Person',
'name' => $author_name,
'url' => $author_url,
),
'publisher' => site_schema_organization(),
);
if ( $image_url ) {
$schema['image'] = array( $image_url );
}
if ( $article_section ) {
$schema['articleSection'] = $article_section;
}
if ( $keywords ) {
$schema['keywords'] = implode( ', ', $keywords );
}
site_schema_output( $schema );
}
add_action( 'wp_head', 'site_blogposting_schema', 20 );
/**
* 产品详情页 Schema
* Product
*
* 默认适配:
* Post Type: products
* Taxonomy: product_cat
* 图片字段: product_mainimg01 ~ product_mainimg08
*/
function site_product_schema() {
if ( ! is_singular( 'products' ) ) return;
$post_id = get_queried_object_id();
$post = get_post( $post_id );
if ( ! $post ) return;
$url = get_permalink( $post_id );
$name = wp_strip_all_tags( get_the_title( $post_id ) );
$description = wp_strip_all_tags( get_the_excerpt( $post_id ) );
if ( empty( $description ) ) {
$description = wp_trim_words(
wp_strip_all_tags(
strip_shortcodes( $post->post_content )
),
50
);
}
$images = array();
for ( $i = 1; $i <= 8; $i++ ) {
$meta_key = 'product_mainimg' . str_pad( $i, 2, '0', STR_PAD_LEFT );
$image_url = get_post_meta( $post_id, $meta_key, true );
if ( $image_url ) {
$images[] = esc_url_raw( $image_url );
}
}
if ( empty( $images ) ) {
$featured_image = get_the_post_thumbnail_url( $post_id, 'full' );
if ( $featured_image ) {
$images[] = esc_url_raw( $featured_image );
}
}
$images = array_values(
array_unique(
array_filter( $images )
)
);
$terms = get_the_terms( $post_id, 'product_cat' );
$category = ( $terms && ! is_wp_error( $terms ) ) ? $terms[0]->name : '';
$schema = array(
'@context' => 'https://schema.org',
'@type' => 'Product',
'@id' => $url . '#product',
'mainEntityOfPage' => array(
'@type' => 'WebPage',
'@id' => $url,
),
'name' => $name,
'url' => $url,
'brand' => site_schema_organization(),
);
if ( $description ) {
$schema['description'] = $description;
}
if ( $images ) {
$schema['image'] = $images;
}
if ( $category ) {
$schema['category'] = $category;
}
site_schema_output( $schema );
}
add_action( 'wp_head', 'site_product_schema', 20 );
/**
* 普通 Page 页面 Schema
* WebPage
*/
function site_page_schema() {
if ( ! is_page() || is_front_page() ) return;
$post_id = get_queried_object_id();
$url = get_permalink( $post_id );
$name = wp_strip_all_tags( get_the_title( $post_id ) );
$home_url = home_url( '/' );
$description = wp_strip_all_tags( get_the_excerpt( $post_id ) );
$image_url = get_the_post_thumbnail_url( $post_id, 'full' );
$schema = array(
'@context' => 'https://schema.org',
'@type' => 'WebPage',
'@id' => $url . '#webpage',
'url' => $url,
'name' => $name,
'isPartOf' => array(
'@id' => $home_url . '#website',
),
'about' => array(
'@id' => $home_url . '#organization',
),
);
if ( $description ) {
$schema['description'] = $description;
}
if ( $image_url ) {
$schema['primaryImageOfPage'] = array(
'@type' => 'ImageObject',
'url' => $image_url,
);
}
site_schema_output( $schema );
}
add_action( 'wp_head', 'site_page_schema', 20 );首页使用:
Organization + WebSite
自动读取:
| WordPress 数据 | Schema |
|---|---|
| Site Title | name |
| 首页 URL | url |
| Custom Logo | logo |
结构示例:
{
"@type": "Organization",
"@id": "https://www.example.com/#organization",
"name": "Example",
"url": "https://www.example.com/",
"logo": {
"@type": "ImageObject",
"url": "https://www.example.com/logo.png"
}
}用于建立:
WebSite
↓ publisher
Organization
不要为了增加字段而随意把 WordPress Tagline 当作 alternateName。
alternateName 更适合真正存在的品牌别名、简称或网站别名。
默认作用于:
is_singular( 'post' )自动映射:
| WordPress 数据 | Schema |
|---|---|
| 标题 | headline |
| 摘要 | description |
| 发布时间 | datePublished |
| 修改时间 | dateModified |
| Author | author |
| Featured Image | image |
| 第一个 Category | articleSection |
| Tags | keywords |
| Site Title / Logo | publisher |
如果文章没有手动摘要,则会从正文截取描述。
建议始终保证:
WordPress Post Author
=
页面显示作者
=
BlogPosting author
不要在页面模板中写死一个作者,而后台文章使用另一个 Author。
当前版本主要针对 B2B 企业官网产品页。
默认配置:
Post Type
products
Taxonomy
product_cat
Images
product_mainimg01
product_mainimg02
...
product_mainimg08
如果自定义产品图片不存在,则自动回退到 Featured Image。
| 产品数据 | Schema |
|---|---|
| 产品标题 | name |
| 摘要 / 正文 | description |
| 产品 URL | url |
| 产品图片 | image |
| 产品分类 | category |
| Site Title | brand |
这套 Product Schema 不依赖 WooCommerce,特别适合:
- 企业官网产品展示
- B2B 询盘型产品站
- 自定义 Post Type 产品
- 没有公开价格的产品目录
- 工厂 / 制造商官网
普通 Page 默认输出:
WebPage
并排除首页:
if ( ! is_page() || is_front_page() ) return;自动映射:
| WordPress 数据 | Schema |
|---|---|
| Page Title | name |
| URL | url |
| Excerpt | description |
| Featured Image | primaryImageOfPage |
| 当前网站 | isPartOf → WebSite |
| 当前企业 | about → Organization |
很多企业官网使用:
- 自定义 Page Template
- Elementor
- Gutenberg
- PHP 模板硬编码内容
页面正文不一定完整存在于:
$post->post_content因此通用版本不强制从正文生成 description,避免 Schema 与实际页面内容不一致。
企业 Page 常见类型:
About Us
Contact Us
Factory
R&D
Quality Control
Free Samples
它们通常属于企业实体,而不是具体作者。
因此普通 Page 不默认输出:
Person
author
普通企业 Page 通常长期维护,而且页面前端往往不显示发布日期。
因此 v1.0.0 不默认输出:
datePublished
dateModified
Product Schema 可以描述产品实体,但如果希望获得 Google Product Snippet 富媒体结果,通常还需要真实存在的:
offers
review
aggregateRating
如果当前 B2B 网站:
- 没有公开价格
- 没有真实评论
- 没有真实评分
不要为了通过测试而伪造:
price
availability
review
rating
当前方案有意不输出这些字段。
因此:
ProductSchema 本身仍然成立,但页面不一定具备 Google 商品富媒体结果展示资格。
如果以后网站增加真实价格,可以再扩展:
'offers' => array(
'@type' => 'Offer',
'price' => '29.90',
'priceCurrency' => 'USD',
'availability' => 'https://schema.org/InStock',
'url' => $url,
),前提是这些数据必须与页面实际显示内容一致。
默认:
site_
正式项目建议替换为唯一前缀。
默认:
is_singular( 'products' )如果其他网站使用:
product
catalog
portfolio
需要对应调整。
默认:
product_cat
其他网站如果 Taxonomy 名称不同,需要修改。
默认:
product_mainimg01 ~ product_mainimg08
如果其他项目只使用 Featured Image 或其他 Meta 字段,只需要调整产品图片读取部分。
当前默认:
WordPress Site Title = 产品品牌
如果网站销售多个品牌,建议单独增加产品 Brand 字段,而不是直接使用 Site Title。
Page Schema 默认读取:
Page Title
Excerpt
Featured Image
如果某些模板完全把标题、描述写死在 PHP 文件中,需要检查 WordPress 后台数据是否仍然与页面实际内容一致。
没有真实数据来源时,不建议增加:
articleBody
wordCount
commentCount
review
aggregateRating
offers
price
availability
sku
gtin
mpn
sameAs
FAQPage
判断标准:
有真实、稳定、长期可维护的数据来源,再输出。
部署后先查看网页源代码:
Ctrl + U
搜索:
application/ld+json
分别确认:
Organization
WebSite
BlogPosting
Product
WebPage
然后使用:
- Google Rich Results Test
- Schema.org Validator
- Google Search Console URL Inspection
进行进一步验证。
对于没有:
offers
review
aggregateRating
的 B2B 产品页面,Google Rich Results Test 可能提示缺少 Product Rich Result 所需字段。
这并不等于 JSON-LD 语法错误,而是表示当前页面不满足商品富媒体搜索结果资格。
普通 WebPage 本身也不一定产生特殊 Rich Result,它的主要作用是建立:
WebPage
→ WebSite
→ Organization
之间的语义关系。
不要只维护 Schema。
真正应该维护的是:
后台数据
↓
页面内容
↓
Schema
如果其中一个发生变化,另外两个也应检查。
例如:
产品图片来自哪里?
作者来自哪里?
Page H1 来自哪里?
产品品牌来自哪里?
先确认数据来源,再修改 Schema。
如果未来开始使用 SEO 插件,应重新查看源码,确认是否出现重复:
Organization
WebSite
BlogPosting
Product
WebPage
重复 Schema 本身不一定立即报错,但如果不同来源的数据不一致,会增加搜索引擎理解成本。
v1.0.0 暂时只覆盖核心实体。
以后有明确需求时,可进一步增加:
BreadcrumbList
Author ProfilePage
AboutPage
ContactPage
LocalBusiness
Product Offer
Product Review
每新增一种 Schema,建议先确认:
- 页面是否真实存在对应实体;
- 数据来源是否稳定;
- 是否能够长期维护;
- Google 当前是否仍支持相关展示;
- 是否会与现有 Schema 重复。
适合:
- WordPress 企业官网
- 外贸 B2B 网站
- 工厂官网
- 制造商网站
- 产品询盘型网站
- 自定义主题项目
- 多站复用型 WordPress 项目
如果网站已经高度依赖:
- WooCommerce
- Rank Math
- Yoast SEO
- AIOSEO
- 专业 Schema 插件
建议先检查已有结构化数据,再决定是否需要这套方案。
完成基础 Schema 体系:
OrganizationWebSiteBlogPostingProductWebPage
并建立统一的:
- Organization 数据源
- JSON-LD 输出方法
- WordPress 数据映射
- 多站复用规则
- 验证与维护流程
这套方案并不追求:
Schema 越多越好。
而是希望建立:
一套真实、轻量、通用、稳定、方便长期维护的 WordPress 企业官网 Schema 基础体系。
对于大多数 B2B 企业官网来说,先把这四类核心页面做好:
首页
博客
产品
普通 Page
已经比堆叠大量难以维护的结构化数据更有价值。