I was searching for a way to add structured data to my website and came across this thread. Google prefers JSON-LD so it didn't quite meet my requirements. I eventually figured out how to add it using JSON.
Functions File:
function schema_mark_up () {
while ( have_posts() ) : the_post();
$post_id = get_the_ID();
$name = get_the_title();
$permalink = get_permalink();
$images = array (get_post_meta($post_id, 'banner_picture', true),
get_post_meta($post_id, 'profile_picture', true)
);
$street_address = array (
get_post_meta($post_id, 'address', true),
get_post_meta($post_id, 'address_1', true)
);
$address_locality = get_post_meta($post_id, 'Suburb', true);
$address_locality = get_post_meta($post_id, 'Province', true);
$postal_code = get_post_meta($post_id, 'Postal Code', true);
$address_country = get_post_meta($post_id, 'Country', true);
$telephone = get_post_meta($post_id, 'Contact Number', true);
$data = array (
'@context' => 'http://www.schema.org',
'@type' => 'LocalBusiness',
'image' => $images,
'@id' => $permalink,
'name' => $name,
'address' => array (
'@type' => 'PostalAddress',
'streetAddress' => $street_address,
'addressLocality' => $address_locality,
'addressRegion' => $address_region,
'postalCode' => $postal_code,
'addressCountry' => $address_country
),
'priceRange' => 'ZAR',
'telephone' => $telephone
);
$json = json_encode($data);
echo $json;
endwhile;
}
In your header or whereever you want to call the code:
<script type="application/ld+json">
<?php schema_markup (); ?>
</script>