如何在 PHP 代码 WordPress 中添加 SportsEvent 模式 Json-ld 代码
How to add SportsEvent schema Json-ld code inside PHP code WordPress
我是一名尝试为我的网站整理 SportsEvent 架构代码的初学者。我已经设法做到这一点。我创建了自定义字段并将它们映射到架构属性。我似乎无法超越这一点。我在激活插件时 运行 遇到错误 - Parse error: syntax error, unexpected ';', expecting ')' in /home/uhkcj8xz70dl/public_html/wp-content/plugins/football-schema/football_schema.php on line 21
<?php
/**
* Plugin Name: Schema.org for Football
* Description: Add SportsEvent Schema.org in JSONld to site
* Plugin URI:
* Author: Danstan
* Author URI:
* Version: 1.0.0
* License: GPL2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
function addschema() //Function for Schema.org
{
global $post;
if (is_singular('matches')) { //only for post type matches
$schema_sportsevent = array(
'@context' => "http://schema.org",
'@type' => "SportsEvent",
'name' => get_the_title($post->ID);
'description' => get_the_content($post->ID);
'url' => get_permalink();
'startDate' => get_post_meta( get_the_ID(), 'start_date' );
'endDate'=> get_post_meta( get_the_ID(), 'end_date' );
'image' => get_the_post_thumbnail_url($post->ID);
'competitor' => array(
'@type' => "SportsTeam",
'name' => "Team A",
'image' => get_post_meta( get_the_ID(), 'logo_left' );
),
'competitor' => array(
'@type' => "SportsTeam",
'name' => "Team B",
'image' => get_post_meta( get_the_ID(), 'logo_right' );
),
'location' => array(
'@type' => "Place",
'name' => get_post_meta( get_the_ID(), 'venue_name' );
'address' => array(
'@type' => "PostalAddress",
'postalCode' => get_post_meta( get_the_ID(), 'zip_postal_code' );
'streetAddress' => get_post_meta( get_the_ID(), 'street_address' );
'addressLocality' => get_post_meta( get_the_ID(), 'locality_city' );
'addressRegion' => get_post_meta( get_the_ID(), 'address_region' );
'addressCountry' => get_post_meta( get_the_ID(), 'country' );
)
)
);
echo '<script type="application/ld+json">' . json_encode($schema_sportsevent) . '</script>';
//encode schema for matches
}
endif;
}
add_action('wp_head', 'addschema'); //Add Schema to header
?>`
您在需要 ,
的地方混入了一些 ;
,因为它是一个值数组,您在不需要时添加了一个 endif。试试这个。
function addschema() //Function for Schema.org
{
global $post;
if (is_singular('post')) { //only for post type matches
$schema_sportsevent = array(
'@context' => "http://schema.org",
'@type' => "SportsEvent",
'name' => get_the_title($post->ID),
'description' => get_the_content($post->ID),
'url' => get_permalink(),
'startDate' => get_post_meta( get_the_ID(), 'start_date' ),
'endDate'=> get_post_meta( get_the_ID(), 'end_date' ),
'image' => get_the_post_thumbnail_url($post->ID),
'competitor' => array(array(
'@type' => "SportsTeam",
'name' => "Team A",
'image' => get_post_meta( get_the_ID(), 'logo_left' )),
array('@type' => "SportsTeam",
'name' => "Team B",
'image' => get_post_meta( get_the_ID(), 'logo_left' ))
),
'location' => array(
'@type' => "Place",
'name' => get_post_meta( get_the_ID(), 'venue_name' ),
'address' => array(
'@type' => "PostalAddress",
'postalCode' => get_post_meta( get_the_ID(), 'zip_postal_code' ),
'streetAddress' => get_post_meta( get_the_ID(), 'street_address' ),
'addressLocality' => get_post_meta( get_the_ID(), 'locality_city' ),
'addressRegion' => get_post_meta( get_the_ID(), 'address_region' ),
'addressCountry' => get_post_meta( get_the_ID(), 'country' ),
)
)
);
echo '<script type="application/ld+json">' . json_encode($schema_sportsevent) . '</script>';
//encode schema for matches
}
}
应该像这样生成 JSON。虽然我的 post 没有一些 post_meta 来填补一些碎片。
{
"@context": "http://schema.org",
"@type": "SportsEvent",
"name": "some stuff",
"description": "",
"url": "http://192.168.33.10/wordpress/blog/2019/11/28/some-stuff/",
"startDate": [],
"endDate": [],
"image": false,
"competitor": [
{
"@type": "SportsTeam",
"name": "Team A",
"image": []
},
{
"@type": "SportsTeam",
"name": "Team B",
"image": []
}
],
"location": {
"@type": "Place",
"name": [],
"address": {
"@type": "PostalAddress",
"postalCode": [],
"streetAddress": [],
"addressLocality": [],
"addressRegion": [],
"addressCountry": []
}
}
}
我是一名尝试为我的网站整理 SportsEvent 架构代码的初学者。我已经设法做到这一点。我创建了自定义字段并将它们映射到架构属性。我似乎无法超越这一点。我在激活插件时 运行 遇到错误 - Parse error: syntax error, unexpected ';', expecting ')' in /home/uhkcj8xz70dl/public_html/wp-content/plugins/football-schema/football_schema.php on line 21
<?php
/**
* Plugin Name: Schema.org for Football
* Description: Add SportsEvent Schema.org in JSONld to site
* Plugin URI:
* Author: Danstan
* Author URI:
* Version: 1.0.0
* License: GPL2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
function addschema() //Function for Schema.org
{
global $post;
if (is_singular('matches')) { //only for post type matches
$schema_sportsevent = array(
'@context' => "http://schema.org",
'@type' => "SportsEvent",
'name' => get_the_title($post->ID);
'description' => get_the_content($post->ID);
'url' => get_permalink();
'startDate' => get_post_meta( get_the_ID(), 'start_date' );
'endDate'=> get_post_meta( get_the_ID(), 'end_date' );
'image' => get_the_post_thumbnail_url($post->ID);
'competitor' => array(
'@type' => "SportsTeam",
'name' => "Team A",
'image' => get_post_meta( get_the_ID(), 'logo_left' );
),
'competitor' => array(
'@type' => "SportsTeam",
'name' => "Team B",
'image' => get_post_meta( get_the_ID(), 'logo_right' );
),
'location' => array(
'@type' => "Place",
'name' => get_post_meta( get_the_ID(), 'venue_name' );
'address' => array(
'@type' => "PostalAddress",
'postalCode' => get_post_meta( get_the_ID(), 'zip_postal_code' );
'streetAddress' => get_post_meta( get_the_ID(), 'street_address' );
'addressLocality' => get_post_meta( get_the_ID(), 'locality_city' );
'addressRegion' => get_post_meta( get_the_ID(), 'address_region' );
'addressCountry' => get_post_meta( get_the_ID(), 'country' );
)
)
);
echo '<script type="application/ld+json">' . json_encode($schema_sportsevent) . '</script>';
//encode schema for matches
}
endif;
}
add_action('wp_head', 'addschema'); //Add Schema to header
?>`
您在需要 ,
的地方混入了一些 ;
,因为它是一个值数组,您在不需要时添加了一个 endif。试试这个。
function addschema() //Function for Schema.org
{
global $post;
if (is_singular('post')) { //only for post type matches
$schema_sportsevent = array(
'@context' => "http://schema.org",
'@type' => "SportsEvent",
'name' => get_the_title($post->ID),
'description' => get_the_content($post->ID),
'url' => get_permalink(),
'startDate' => get_post_meta( get_the_ID(), 'start_date' ),
'endDate'=> get_post_meta( get_the_ID(), 'end_date' ),
'image' => get_the_post_thumbnail_url($post->ID),
'competitor' => array(array(
'@type' => "SportsTeam",
'name' => "Team A",
'image' => get_post_meta( get_the_ID(), 'logo_left' )),
array('@type' => "SportsTeam",
'name' => "Team B",
'image' => get_post_meta( get_the_ID(), 'logo_left' ))
),
'location' => array(
'@type' => "Place",
'name' => get_post_meta( get_the_ID(), 'venue_name' ),
'address' => array(
'@type' => "PostalAddress",
'postalCode' => get_post_meta( get_the_ID(), 'zip_postal_code' ),
'streetAddress' => get_post_meta( get_the_ID(), 'street_address' ),
'addressLocality' => get_post_meta( get_the_ID(), 'locality_city' ),
'addressRegion' => get_post_meta( get_the_ID(), 'address_region' ),
'addressCountry' => get_post_meta( get_the_ID(), 'country' ),
)
)
);
echo '<script type="application/ld+json">' . json_encode($schema_sportsevent) . '</script>';
//encode schema for matches
}
}
应该像这样生成 JSON。虽然我的 post 没有一些 post_meta 来填补一些碎片。
{
"@context": "http://schema.org",
"@type": "SportsEvent",
"name": "some stuff",
"description": "",
"url": "http://192.168.33.10/wordpress/blog/2019/11/28/some-stuff/",
"startDate": [],
"endDate": [],
"image": false,
"competitor": [
{
"@type": "SportsTeam",
"name": "Team A",
"image": []
},
{
"@type": "SportsTeam",
"name": "Team B",
"image": []
}
],
"location": {
"@type": "Place",
"name": [],
"address": {
"@type": "PostalAddress",
"postalCode": [],
"streetAddress": [],
"addressLocality": [],
"addressRegion": [],
"addressCountry": []
}
}
}