Created
February 18, 2020 13:24
-
-
Save izzygld/d8e8152551a416259ae8a09266e8f4ba to your computer and use it in GitHub Desktop.
Structured data - wordpress theme
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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