Yêu cầu
- Trang cần dùng template có slug là
template-tools.php - Schema cần chuyển thành
WebApplicationvà bổ sung thông tin kỹ thuật
1. Thay đổi loại schema bằng filter init_plugin_suite_review_system_schema_type
Đoạn code sau sẽ kiểm tra xem post type có phải là page và template có phải là template-tools.php hay không. Nếu đúng, nó sẽ đổi schema thành WebApplication.
add_filter( 'init_plugin_suite_review_system_schema_type', function( $type, $post_type ) {
$post_id = get_the_ID();
if ( 'page' === $post_type && get_page_template_slug( $post_id ) === 'template-tools.php' ) {
return 'WebApplication';
}
return $type;
}, 10, 2 );
2. Bổ sung dữ liệu mở rộng bằng filter init_plugin_suite_review_system_schema_data
Khi schema đã được đổi sang WebApplication, bạn có thể mở rộng dữ liệu JSON-LD bằng các trường như applicationCategory, operatingSystem, và offers. Đây là những trường phù hợp với các ứng dụng web miễn phí.
add_filter( 'init_plugin_suite_review_system_schema_data', function( $schema, $post_id, $type ) {
if ( $type === 'WebApplication' ) {
$schema['applicationCategory'] = 'WebApplication';
$schema['operatingSystem'] = 'All';
$schema['browserRequirements'] = 'Modern browser with JavaScript enabled';
$schema['offers'] = [
'@type' => 'Offer',
'price' => '0',
'priceCurrency' => 'USD',
];
}
return $schema;
}, 10, 3 );
Kết quả
Khi người dùng truy cập trang có template template-tools.php, plugin sẽ tự động render đoạn schema JSON-LD như sau trong HTML:
{
"@context": "https://schema.org",
"@type": "WebApplication",
"name": "Tiêu đề trang",
"aggregateRating": {
"@type": "AggregateRating",
"ratingValue": 4.9,
"reviewCount": 182,
"bestRating": 5
},
"applicationCategory": "WebApplication",
"operatingSystem": "All",
"browserRequirements": "Modern browser with JavaScript enabled",
"offers": {
"@type": "Offer",
"price": "0",
"priceCurrency": "USD"
}
}
Việc khai báo đúng loại schema sẽ giúp Google hiểu rõ hơn nội dung trang bạn, đồng thời mở ra khả năng hiển thị rich snippet cho ứng dụng web trong kết quả tìm kiếm.
Bình luận