Service Structured Data for a Services Post Type
Adds Service JSON-LD (name, description, provider and area served) to single service pages, describing what your business offers.
Opens WPIDE on
Code
// Post type and the areas you serve.
$wpide_service_type = 'service';
$wpide_service_areas = array( 'Springfield', 'Shelbyville' );
add_action( 'wp_head', function () use ( $wpide_service_type, $wpide_service_areas ) {
if ( ! is_singular( $wpide_service_type ) ) {
return;
}
$post = get_queried_object();
$data = array(
'@context' => 'https://schema.org',
'@type' => 'Service',
'name' => wp_strip_all_tags( get_the_title( $post ) ),
'serviceType' => wp_strip_all_tags( get_the_title( $post ) ),
'description' => wp_strip_all_tags( get_the_excerpt( $post ) ),
'url' => get_permalink( $post ),
'image' => get_the_post_thumbnail_url( $post, 'full' ),
'provider' => array( '@type' => 'Organization', 'name' => get_bloginfo( 'name' ), 'url' => home_url( '/' ) ),
'areaServed' => array_map( function ( $area ) {
return array( '@type' => 'Place', 'name' => $area );
}, $wpide_service_areas ),
);
echo '<script type="application/ld+json">' . wp_json_encode( array_filter( $data ), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_HEX_TAG ) . "</script>\n";
} );
Notes
Made for the Services post type snippet. Set the areas you serve.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.