I would like to add the right rich text snippets so that the Goole Structured Data validator will pass all the code I have. The only things missing are the sizes of the thumbnails.
This specific examples is for the thumbnails in the loop of the WordPress homepage.
I have this code:
<div itemprop="image" itemscope itemtype="https://schema.org/ImageObject">
<meta itemprop="url" content="<?php echo wp_get_attachment_thumb_url( get_post_thumbnail_id( $post->ID ) ); ?>">
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail( 'full', array('class'=>'post_thumbnail_common', 'alt' => get_the_title() , 'title' => get_the_title() ));
echo contentnoimg(41);} else { echo content(41); } ?>
</div>
Here I want to add the following:
<meta itemprop="width" content="800">
<meta itemprop="height" content="800">
So that the final code will look something like this:
<div itemprop="image" itemscope itemtype="https://schema.org/ImageObject">
<meta itemprop="url" content="<?php echo wp_get_attachment_thumb_url( get_post_thumbnail_id( $post->ID ) ); ?>">
<meta itemprop="width" content="<?php XXX ?>">
<meta itemprop="height" content="<?php XXX ?>">
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail( 'full', array('class'=>'post_thumbnail_common', 'alt' => get_the_title() , 'title' => get_the_title() ));
echo contentnoimg(41);} else { echo content(41); } ?>
</div>
How should I put instead of so that the image height and width will be well collected?
Here are the content and contentnoimg functions – they format the excerpt shown after the thumbnail and crop it after X characters:
function content( $limit ) {
global $post;
if( has_excerpt() ){
$content = the_excerpt();
} else {
$content = explode( ' ', get_the_content(), $limit );
if ( count($content) >= $limit ) {
array_pop( $content );
$content = implode( " ", $content );
$content = wp_strip_all_tags( $content, true );
// $content .= '...<br><a href="'. get_permalink($post->ID) . '" class="awesomebtn">'.__('Read full post','language') .'</a>';
} else {
$content = implode( " ", $content );
}
$content = preg_replace( '/\[.+\]/','', $content );
$content = apply_filters( 'the_content', $content );
$content = str_replace( ']]>', ']]>', $content );
}
return $content;
}
function contentnoimg($limit) {
global $post;
if( has_excerpt() ){
$content = the_excerpt();
} else {
$content = explode(' ', get_the_content(), $limit);
if (count($content)>=$limit) {
array_pop($content);
$content = implode(" ",$content);
$content = wp_strip_all_tags($content, true);
// $content .= '...<br><a href="'. get_permalink($post->ID) . '" class="awesomebtn">'.__('Read full post','language') .'</a>';
} else {
$content = implode(" ",$content);
}
$content = preg_replace('/(<img.+?>)/','', $content);
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
}
return $content;
}
Thanks