Skip to content

Instantly share code, notes, and snippets.

@ahmedkaludi
Created June 19, 2016 14:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmedkaludi/b7b236728b9aea58d14ed8d78310f094 to your computer and use it in GitHub Desktop.
Save ahmedkaludi/b7b236728b9aea58d14ed8d78310f094 to your computer and use it in GitHub Desktop.
Structured data in Divi theme
<?php
add_action( 'wp_enqueue_scripts', 'ahmedkaludi_enqueue_styles' );
function ahmedkaludi_enqueue_styles() {
wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
}
// Display Tag code
function ahmedkaludi_structured_data_keyword() {
if ( is_single() ) {
$tags = '"keywords" : [' . strip_tags(get_the_tag_list('"','", "','"')) . ']';
} else { }
echo $tags;
}
function ahmedkaludi_structured_data_output() {
if ( is_single() ) {
$article_author_name = get_the_author_meta( 'nickname', $author_id );
$article_published_date = get_the_time('c');
$thumb_id = get_post_thumbnail_id();
$thumb_url = wp_get_attachment_image_src($thumb_id,'large', true);
?>
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "Article",
"headline": "<?php the_title(); ?>",
"image": {
"@type": "ImageObject",
"url": "<?php echo $thumb_url[0]; ?>",
"width": 1024,
"height": 1024
},
<?php
if(function_exists('ahmedkaludi_structured_data_keyword'))
{
ahmedkaludi_structured_data_keyword();
};
?>,
"datePublished": "<?php echo $article_published_date ?>",
"dateModified": "<?php echo $article_published_date ?>",
"articleSection": "<?php $categories = get_the_category(); foreach($categories as $category) { $cat_name = $category->name; echo $cat_name ; } ?>",
"publisher":{
"@type": "Organization",
"name": "<?php echo $article_author_name ?>",
"logo": {
"@type": "ImageObject",
"url": "http://ahmedkaludi.com/logo.jpg",
"width": 600,
"height": 60
}
},
"author": {
"@type": "Person",
"name": "<?php echo $article_author_name ?>"
},
"articleBody": "<?php $excerpt = strip_tags(get_the_excerpt()); echo $excerpt; ?>",
"mainEntityOfPage": "True"
}
</script>
<?php }
}
add_action('wp_footer','ahmedkaludi_structured_data_output');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment