好的,我想我理解你的要求,请告诉我我在左外野。
首先,您需要看看以下内容:https://aarontgrogg.com/blog/2015/12/16/getting-to-know-and-adding-json-ld-to-a-wordpress-site/
现在我专门使用它来获取模式数据,但我相信您可以提供其他类型的有效负载,例如javascript。
接下来的主要问题是如何将自定义字段数据获取到有效负载中,我想这就是您真正想问的问题?
无论如何,使用该链接中描述的方法来创建json,我已经想到了这个问题-
<?php include(\'json-ld.php\');?>
<script type="application/ld+json"><?php echo json_encode($payload); ?></script>
然后在我的json ld中。php文件我有:
<?php
// JSON - LD for Reaction
// Global Call
function get_post_data() {
global $post;
return $post;
}
$post_data = get_post_data();
$payload["@context"] = "http://schema.org/";
//blog posts
if (is_singular(\'post\')) {
//variables for the post
$post_url = get_permalink();
$post_thumb = wp_get_attachment_url(get_post_thumbnail_id($post->ID));
$excerpt= get_the_excerpt($post->ID); // grab the post excerpt.
$author_select = get_field(\'author\', $post->ID);
$moddatetime = new DateTime($post_data->post_date);
// getting the team member
if ($author_select == \'reaction\') {
$postauthor = \'Reaction\';
}
else if ($author_select == \'guest\') {
$postauthor = get_field(\'guest_writer_name\', $post->ID);
} else {
$author = get_field(\'team_member\', $post->ID);
foreach($author as $member) {
$postauthor = $member->post_title;
}
}
// getting the tags
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
$value .= $tag->name . \', \';
}
}
$value = rtrim($value,\', \');
//payload
$payload["@type"] = "blogPost";
$payload["url"] = $post_url;
$payload["creator"] = array(
"@type" => "Person",
"name" => $postauthor, // outputs my post author as per the calls above
);
$payload["headline"] = $post_data->post_title; // works
$payload["datePublished"] = $moddatetime->format(DateTime::ISO8601); // works
$payload["image"] = $post_thumb;
$payload["description"] = $excerpt; // works
$payload["about"] = $value; // works
}
// home page
if (is_front_page()) {
$logourl = get_site_url();
$payload["@type"] = "Organization";
$payload["name"] = "Reaction Marketing";
$payload["logo"] = $logourl . \'/reaction-logo.png\';
$payload["url"] = get_site_url();
$payload["sameAs"] = array("http://twitter.com/reaction", "https://www.facebook.com/ReactionMarketing", "http://www.linkedin.com/company/reaction-marketing");
$payload["contactPoint"] = array(array("@type" => "ContactPoint", "telephone" => "+1 403 346 6580", "email" => "[email protected]", "contactType" => "sales"));
}
?>
我的最终结果是一个适合页面和帖子的模式。主页输出示例如下:
<script type=\'application/ld+json\'>{"@context":"http:\\/\\/schema.org","@type":"WebSite","url":"http:\\/\\/reaction.ca\\/","name":"Reaction","alternateName":"Reaction","potentialAction":{"@type":"SearchAction","target":"http:\\/\\/reaction.ca\\/?s={search_term_string}","query-input":"required name=search_term_string"}}</script>
<script type=\'application/ld+json\'>{"@context":"http:\\/\\/schema.org","@type":"Organization","url":"http:\\/\\/reaction.ca\\/","sameAs":["https:\\/\\/www.facebook.com\\/ReactionMarketing","https:\\/\\/www.instagram.com\\/reaction","https:\\/\\/www.linkedin.com\\/company\\/reaction-marketing","https:\\/\\/twitter.com\\/reaction"],"name":"Reaction Marketing","logo":"http:\\/\\/reaction.ca\\/uploads\\/REACTION-RGB.jpg"}</script>
现在,很明显,这是特定于模式的,但您可以在其中看到,我已经调用了特定于各种自定义字段的数据(使用ACF)。您可以在那里的博客帖子中看到我的变量是如何被调用的。
这似乎符合你的要求吗?