i want to add a dynamic JSON-LD (schema) for career posts in my wordpress page. I tried this code below. But i think i have some Syntax errors within the array. Maybe the "echo" is not allowed within the array? I hope somebody can help me with this?
<?php function schema() {?>
<?php if( have_rows('schema_auszeichnung') ): ?>
<?php while( have_rows('schema_auszeichnung') ): the_row();
// Get sub field values.
$title = get_sub_field('title');
$description = get_sub_field('description');
$state = get_sub_field('state');
$date = get_sub_field('date');
$street = get_sub_field('street');
$city = get_sub_field('city');
$postalcode = get_sub_field('postalcode');
$schema = array(
'@context' => '',
'@type' => 'JobPosting',
'title' => $title,
'description' => $description,
'hiringOrganization' => array(
'@type' => 'Organization',
'name' => 'cent GmbH',
'sameAs' => get_home_url(),
'logo' => '/wp/wp-content/uploads/2016/11/cropped-logo.png'
),
'employmentType'=> $state,
'datePosted' => $date,
'validThrough' => "",
'jobLocation' => array(
'@type' => "Place",
'address' => array (
'@type' => 'PostalAddress',
'streetAddress' => $street,
'adressLocality' => $city,
'postalCode' => $postalcode,
'addressCountry' => 'DE'
),
),
);
?>
<?php endwhile; ?>
<?php endif; ?>
<?php
echo '<script type="application/ld+json">' . json_encode($schema) . '</script>';
}
add_action('wp_head', 'schema');
?>
EDIT:
After removing the 'echo' i got the php warning: Undefined variable: schema. But for me it is defined in this line: $schema = array.....
i want to add a dynamic JSON-LD (schema.org) for career posts in my wordpress page. I tried this code below. But i think i have some Syntax errors within the array. Maybe the "echo" is not allowed within the array? I hope somebody can help me with this?
<?php function schema() {?>
<?php if( have_rows('schema_auszeichnung') ): ?>
<?php while( have_rows('schema_auszeichnung') ): the_row();
// Get sub field values.
$title = get_sub_field('title');
$description = get_sub_field('description');
$state = get_sub_field('state');
$date = get_sub_field('date');
$street = get_sub_field('street');
$city = get_sub_field('city');
$postalcode = get_sub_field('postalcode');
$schema = array(
'@context' => 'http://schema.org',
'@type' => 'JobPosting',
'title' => $title,
'description' => $description,
'hiringOrganization' => array(
'@type' => 'Organization',
'name' => 'cent GmbH',
'sameAs' => get_home_url(),
'logo' => '/wp/wp-content/uploads/2016/11/cropped-logo.png'
),
'employmentType'=> $state,
'datePosted' => $date,
'validThrough' => "",
'jobLocation' => array(
'@type' => "Place",
'address' => array (
'@type' => 'PostalAddress',
'streetAddress' => $street,
'adressLocality' => $city,
'postalCode' => $postalcode,
'addressCountry' => 'DE'
),
),
);
?>
<?php endwhile; ?>
<?php endif; ?>
<?php
echo '<script type="application/ld+json">' . json_encode($schema) . '</script>';
}
add_action('wp_head', 'schema');
?>
EDIT:
After removing the 'echo' i got the php warning: Undefined variable: schema. But for me it is defined in this line: $schema = array.....
Share Improve this question edited Jul 28, 2020 at 15:11 tom84 asked Jul 28, 2020 at 14:38 tom84tom84 2551 gold badge6 silver badges21 bronze badges2 Answers
Reset to default 2This seems more like a PHP programming question, but yes as you guessed in PHP if you want the value of a variable you just need $var
. echo $var
outputs it and will cause a syntax error how you've used it.
Otherwise everything else looks ok, although obviously I can't say if that data structure will give you valid JSON-LD ;-)
EDIT: Also it's good to give your functions a more unique name than 'schema'
. You could call it 'yourname_jsonld_schema'
or something, so that it's much more unique and unlikely to collide with any other names in Wordpress
EDIT2: To address the extra error you added to your question, the place you define $schema
is inside an if
statement, which means that if the variable is not defined later, that if
was not true. So you need to do something like figure out why the if
is false, or if it's correct, put the place where you use the $schema
inside the if
too.
Thanks so much for this! I was trying to solve almost the same problem, but to pull ACF from an FAQ page and add a structured data page for rich snippets when people google us: https://developers.google.com/search/docs/advanced/structured-data/faqpage
Below is my solution
<?php
$faq_page_schema = '';
if( have_rows('questions') ):
while ( have_rows('questions') ) : the_row();
$question = '"'.get_sub_field('question').'"';
$answer = '"'.get_sub_field('answer').'"';
$faq_page_schema .= "{
\"@type\": \"Question\",
\"name\": $question,
\"acceptedAnswer\": {
\"@type\": \"Answer\",
\"text\": $answer
}
}, ";
endwhile;
else :
// no rows found
endif;
$faq_page_schema = substr_replace($faq_page_schema ,"", -2);
echo
'<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "FAQPage",
"mainEntity": ['.$faq_page_schema.']
}
</script>';
?>