0

I have a website made with wordpress which includes recipes. Now I would like to add the "photo" hrecipe Microformat tag in order to have a thumbnail on Google searches (hrecipe photo tag)

My recipes pictures come from the featured image section. How can I now add itemprop="photo" to the <img> tag?

For example:

  <img itemprop="photo" src="apple-pie.jpg" /> 

This is were my Thumbnail is created:

<div class="post-thumb">

    <?php if( !is_singular() ) { ?>
        <a title="<?php printf(__('Permanent Link to %s', 'zilla'), get_the_title()); ?>" href="<?php the_permalink(); ?>"><?php the_post_thumbnail('blog-large'); ?></a>

    <?php } else {
        the_post_thumbnail('blog-large');

    } ?>

</div>
2

Use the filter post_thumbnail_html. The callback takes 5 parameters, the first ($html) is the one that's returned and that we modify, and the others can be helpful to build the modification.

add_filter( 'post_thumbnail_html', 'add_tag_to_featured_wpse_95469', 10, 5 );

function add_tag_to_featured_wpse_95469( $html, $post_id, $post_thumbnail_id, $size, $attr )
{
    $modify = str_replace( '<img', '<img itemprop="photo"', $html );
    return $modify;
}

Relevant Q&A: Where to put my code: plugin or functions.php?

1

from Adding schema itemprop image to the_post_thumbnail with filters :

add_filter('wp_get_attachment_image_attributes', 'ipwp_img_attr', 10, 2);
function ipwp_img_attr($attr) {
  $attr['itemprop'] = 'photo';
  // OR better 
  // $attr['itemprop'] = 'image';
  return $attr;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.