I would like to add blogposting schema markup so each new post is automatically optimized for rich data. Some code on the web is messy and needs updates per each post, also plugin is not best option as this is custom theme we are trying to keep plugin requirements to the minimum. Is there a code that I can add to single.php file so each page has proper structured data without plugins or additional changes in admin or per post. Thanks!
I would like to add blogposting schema markup so each new post is automatically optimized for rich data. Some code on the web is messy and needs updates per each post, also plugin is not best option as this is custom theme we are trying to keep plugin requirements to the minimum. Is there a code that I can add to single.php file so each page has proper structured data without plugins or additional changes in admin or per post. Thanks!
I think the route you want to go is with JSON-LD.
Here's an excellent guide for how to go about that from CSS-Tricks.
Overview:
Identify the elements of schema you're going to need for your posts
Use functions.php (or an included .php file, however you organize your site's additional functions will be just fine, I usually make my own .php file).
Define your json values with the variables from your site and posts in an array (step 5 of the guide). What's great about this is that all of ACF and wordpress is available to you this way. You can call the excerpt, the content, the title, etc.
Run your json script in the head of your post pages.
Example from guide:
add_action('wp_head', function() {
$schema = array(
// Tell search engines that this is structured data
'@context' => "http://schema.org",
// Tell search engines the content type it is looking at
'@type' => get_field('schema_type', 'options'),
// Provide search engines with the site name and address
'name' => get_bloginfo('name'),
'url' => get_home_url(),
// Provide the company address
'telephone' => '+49' . get_field('company_phone', 'options'), //needs country code
'address' => array(
'@type' => 'PostalAddress',
'streetAddress' => get_field('address_street', 'option'),
'postalCode' => get_field('address_postal', 'option'),
'addressLocality' => get_field('address_locality', 'option'),
'addressRegion' => get_field('address_region', 'option'),
'addressCountry' => get_field('address_country', 'option')
)
);
}
Example from guide:
echo '<script type="application/ld+json">' . json_encode($schema) . '</script>';
This is hands down the best method I've found, simply because you can use everything within wordpress that's available to you in a template, including ACF, and you can add conditions to run the script on specific pages.