Skip to content

Instantly share code, notes, and snippets.

@cmandesign
Created April 1, 2019 11:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save cmandesign/75aa728814c79dd1b1da3fabd0807052 to your computer and use it in GitHub Desktop.
Save cmandesign/75aa728814c79dd1b1da3fabd0807052 to your computer and use it in GitHub Desktop.
<?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