Home > Dev > WordPress SEO > Schema Markup for Articles

Schema Markup for Articles

This code adds schema markup for articles.

// Add this to your theme's functions.php file

function add_schema_markup() {
    if (is_single()) {
        global $post;
        $schema = array(
            "@context" => "https://schema.org",
            "@type" => "Article",
            "headline" => get_the_title(),
            "description" => get_the_excerpt(),
            "author" => array(
                "@type" => "Person",
                "name" => get_the_author()
            ),
            "datePublished" => get_the_date('c'),
            "dateModified" => get_the_modified_date('c'),
            "mainEntityOfPage" => array(
                "@type" => "WebPage",
                "@id" => get_permalink()
            ),
            "image" => get_the_post_thumbnail_url($post->ID, 'full')
        );
        echo '<script type="application/ld+json">' . json_encode($schema) . '</script>';
    }
}
add_action('wp_head', 'add_schema_markup');
Back