Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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>
share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

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?

share|improve this answer
add comment

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;
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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