Article structured data for posts
Adds BlogPosting structured data (JSON-LD) to single posts: headline, dates, author, image. Helps Google understand and show your articles. Skipped when an SEO plugin is active.
<?php
add_action(
'wp_head',
static function () {
if ( ! is_singular( 'post' ) || defined( 'WPSEO_VERSION' ) || defined( 'RANK_MATH_VERSION' ) || defined( 'AIOSEO_VERSION' ) || defined( 'SEOPRESS_VERSION' ) || class_exists( 'The_SEO_Framework\\Load' ) ) {
return;
}
$post = get_queried_object();
$data = array(
'@context' => 'https://schema.org',
'@type' => 'BlogPosting',
'headline' => wp_strip_all_tags( get_the_title( $post ) ),
'datePublished' => get_the_date( 'c', $post ),
'dateModified' => get_the_modified_date( 'c', $post ),
'mainEntityOfPage' => get_permalink( $post ),
'author' => array(
'@type' => 'Person',
'name' => get_the_author_meta( 'display_name', (int) $post->post_author ),
),
'publisher' => array(
'@type' => 'Organization',
'name' => get_bloginfo( 'name' ),
),
);
$image = get_the_post_thumbnail_url( $post, 'full' );
if ( $image ) {
$data['image'] = $image;
}
$logo = get_site_icon_url( 512 );
if ( $logo ) {
$data['publisher']['logo'] = array( '@type' => 'ImageObject', 'url' => $logo );
}
echo '<script type="application/ld+json">' . wp_json_encode( $data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_HEX_TAG ) . '</script>' . "\n";
}
);
In these packs
- SEO basics: Meta descriptions, social sharing tags, article structured data and cleaner indexing, for sites without an SEO plugin.