如何以编程方式将架构代码插入wordpress,使其自动适用于目标post类型

如何以编程方式将架构代码插入wordpress,使其自动适用于目标post类型,wordpress,schema.org,json-ld,Wordpress,Schema.org,Json Ld,我需要将这个模式json代码添加到WordPress中。我知道如何将此添加到个人帖子中,但我正在寻找一种情况,在这种情况下,我不需要为每个帖子手动添加此内容。我想插入代码,以便它能自动为我所针对的特定文章类型工作,在本例中,该类型是匹配的。我不知道该怎么办。我有一些自定义字段,我想用php在代码中插入这些字段,但我不确定这样做是否可行。请帮忙,谢谢 <script type="application/ld+json"> { "@context":"http://schema.

我需要将这个模式json代码添加到WordPress中。我知道如何将此添加到个人帖子中,但我正在寻找一种情况,在这种情况下,我不需要为每个帖子手动添加此内容。我想插入代码,以便它能自动为我所针对的特定文章类型工作,在本例中,该类型是匹配的。我不知道该怎么办。我有一些自定义字段,我想用php在代码中插入这些字段,但我不确定这样做是否可行。请帮忙,谢谢

<script type="application/ld+json">
{
    "@context":"http://schema.org",
    "@type":"SportsEvent",
    "name": "Team A vs Team B",
    "description": "Team A vs Team B for League Match",
    "startDate": "2020-03-01",
    "endDate": "2020-03-01",
    "competitor": [
    {
        "@type": "SportsTeam",
        "name": "Team A",
        "image":"https://example.com/wp-content/uploads/2015/03/650.png"
        },
    {
        "@type": "SportsTeam",
        "name": "Team B",
        "image":"https://example.com/wp-content/uploads/2015/03/680.png"
    }
    ],
    "location": {
        "@type": "Place",
        "address": "Stadium of Team A"
    }
}
</script>

{
“@context”:”http://schema.org",
“@type”:“SportsEvent”,
“姓名”:“A队对B队”,
“描述”:“联赛A队对B队”,
“起始日期”:“2020-03-01”,
“截止日期”:“2020-03-01”,
“竞争对手”:[
{
“@type”:“SportsTeam”,
“姓名”:“A队”,
“图像”:https://example.com/wp-content/uploads/2015/03/650.png"
},
{
“@type”:“SportsTeam”,
“姓名”:“B队”,
“图像”:https://example.com/wp-content/uploads/2015/03/680.png"
}
],
“地点”:{
“@type”:“Place”,
“地址”:“A队体育场”
}
}

您可以将此函数添加到主题的
functions.php
文件中

function sports_event_schema() {
echo '
<script type="application/ld+json">
{
    "@context":"http://schema.org",
    "@type":"SportsEvent",
    "name": "Team A vs Team B",
    "description": "Team A vs Team B for League Match",
    "startDate": "2020-03-01",
    "endDate": "2020-03-01",
    "competitor": [
    {
        "@type": "SportsTeam",
        "name": "Team A",
        "image":"https://example.com/wp-content/uploads/2015/03/650.png"
        },
    {
        "@type": "SportsTeam",
        "name": "Team B",
        "image":"https://example.com/wp-content/uploads/2015/03/680.png"
    }
    ],
    "location": {
        "@type": "Place",
        "address": "Stadium of Team A"
    }
}
</script>';
}
if ( 'Matches' == get_post_type() ) {
 add_action('wp_footer', 'sports_event_schema');
}
功能运动事件模式(){
回声'
{
“@context”:”http://schema.org",
“@type”:“SportsEvent”,
“姓名”:“A队对B队”,
“描述”:“联赛A队对B队”,
“起始日期”:“2020-03-01”,
“截止日期”:“2020-03-01”,
“竞争对手”:[
{
“@type”:“SportsTeam”,
“姓名”:“A队”,
“图像”:https://example.com/wp-content/uploads/2015/03/650.png"
},
{
“@type”:“SportsTeam”,
“姓名”:“B队”,
“图像”:https://example.com/wp-content/uploads/2015/03/680.png"
}
],
“地点”:{
“@type”:“Place”,
“地址”:“A队体育场”
}
}
';
}
if('Matches'==get_post_type()){
添加动作('wp_页脚','sports_event_schema');
}
另一个选项是直接在页眉或页脚中添加json,并仅添加if语句:

<?php if ( 'Matches' == get_post_type() ) : ?>
<script type="application/ld+json">
{
    "@context":"http://schema.org",
    "@type":"SportsEvent",
    "name": "Team A vs Team B",
    "description": "Team A vs Team B for League Match",
    "startDate": "2020-03-01",
    "endDate": "2020-03-01",
    "competitor": [
    {
        "@type": "SportsTeam",
        "name": "Team A",
        "image":"https://example.com/wp-content/uploads/2015/03/650.png"
        },
    {
        "@type": "SportsTeam",
        "name": "Team B",
        "image":"https://example.com/wp-content/uploads/2015/03/680.png"
    }
    ],
    "location": {
        "@type": "Place",
        "address": "Stadium of Team A"
    }
}
</script>

<?php endif; ?>

{
“@context”:”http://schema.org",
“@type”:“SportsEvent”,
“姓名”:“A队对B队”,
“描述”:“联赛A队对B队”,
“起始日期”:“2020-03-01”,
“截止日期”:“2020-03-01”,
“竞争对手”:[
{
“@type”:“SportsTeam”,
“姓名”:“A队”,
“图像”:https://example.com/wp-content/uploads/2015/03/650.png"
},
{
“@type”:“SportsTeam”,
“姓名”:“B队”,
“图像”:https://example.com/wp-content/uploads/2015/03/680.png"
}
],
“地点”:{
“@type”:“Place”,
“地址”:“A队体育场”
}
}