0

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!

0

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:

  1. Identify the elements of schema you're going to need for your posts

  2. 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).

  3. 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.

  4. 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.

| improve this answer | |
  • Thanks for you input I did read the CSS Tricks article and it is great article. I guess I will have to get my hands dirty as most of the options from this article is not really for blogs but for business oriented website. I will need to follow options bellow and find appropriate codes for each blog post: schema.org/BlogPosting – Benjamin Franklin Apr 18 '19 at 8:11
  • I guess I was hoping someone already has these to share :) There is forum with some of them, but with errors: help.gulshankumar.net/t/… – Benjamin Franklin Apr 18 '19 at 8:13
  • I have tried to follow guideline, but did not produce any results when testing at rich data Google testing. Maybe I am doing something wrong. – Benjamin Franklin Apr 18 '19 at 11:49
  • Yeah, it is just an example of how to do it, and not a cut and paste solution for blogs specifically. I don't know that you'll find that. – Faye Apr 30 '19 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.