-
-
Save Vincent-gv/606bad11876fbc1f6c23199c65ecccd3 to your computer and use it in GitHub Desktop.
This file contains 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 | |
$featured_image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'img_post' ); | |
// img_post is name of my thumbnail size ( I've added my own in functions.php ) | |
$focuskw = get_post_meta(get_the_ID(), ‘_yoast_wpseo_focuskw’, true); | |
// It's focus keyword that you enter in Yoast Metabox in post edit page, it's really important | |
$primary_cat = get_post_meta(get_the_ID(), ‘_yoast_wpseo_primary_category’, true); | |
// Primary Category is important, for permalink format and meta keywords | |
if($primary_cat == NULL && $primary_cat == ''){ | |
$cats = wp_get_post_categories($post->ID); | |
$primary_cat = $cats[0]; | |
// Wordpress posts always have at least one category. | |
} | |
$primary_cat = get_cat_name($primary_cat); | |
// I am building the template for description as the same as i built before in Yoast SEO Titles section, now i am sure that my Rich snippets are the same as my meta tags which is generated by Yoast. | |
// My Fromat will be like this : | |
// "Wordpress Development (Tutorial),Updated 1 April 2019. For Become professional in wordpress development ..." | |
$description = $focuskw . " (" .$primary_cat. "), Updated " .get_the_date(). ". " . get_the_excerpt(); | |
// Post keywords | |
$tags = get_the_tags(); | |
$tags_string = ""; | |
$stripped_content = esc_attr(wp_strip_all_tags( get_the_content() )); | |
foreach ( $tags as $tag ) { | |
$tags_string .= $tag->name . " "; | |
} | |
$post_date = mysql2date( 'c', $post->post_date, false ); | |
$post_modified_date = mysql2date( 'c', $post->post_modified, false); | |
?> | |
<script type="application/ld+json"> | |
{ | |
"@context": "http://schema.org", | |
"@type": "Article", | |
"mainEntityOfPage": { | |
"@type": "WebPage", | |
"@id": "https://google.com/article" | |
}, | |
"headline": "<?php the_title(); ?>", | |
"alternativeHeadline": "<?php echo $description; ?>", | |
"image": "<?php echo $featured_image[0]; ?>", | |
"author": { | |
"@type": "Person", | |
"name": "<?php the_author(); ?>" | |
}, | |
"publisher": { | |
"@type": "Organization", | |
"name": "MyWebsite", | |
"logo": { | |
"@type": "ImageObject", | |
"url": "https://cdn.myweb.com/logo.jpg" | |
} | |
}, | |
"genre": "<?php echo $primary_cat; ?>", | |
"keywords": "<?php echo $tags_string; ?>", | |
"wordcount": " <?php echo str_word_count($stripped_content); ?>", | |
"url": "<?php the_permalink() ?>", | |
"datePublished": "<?php echo $post_date; ?>", | |
"dateCreated": "<?php echo $post_date; ?>", | |
"dateModified": "<?php echo $post_modified_date; ?>", | |
"description": "<?php echo $description ?>", | |
"articleBody": "<?php echo $stripped_content; ?>", | |
"aggregateRating": { | |
"@type": "AggregateRating", | |
"ratingValue": "<?php echo get_post_meta(get_the_ID(),'ratings_average', true); ?>", | |
"bestRating": "5", | |
"ratingCount": "<?php echo get_post_meta(get_the_ID(), 'ratings_users', true); ?>" | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment