WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to update my <meta property="og:image" content="http://amemoirproject.com/facebook/og-image.png"/> tag in the head with the current post's feature image. How can I do that?

share|improve this question

On a single post page the $post variable should work for you. It is set well before the page header prints. You will need get_post_thumbnail_id and wp_get_attachment_image_src

 if (is_single()) {
    $thumbid = get_post_thumbnail_id($post->ID);
    $imgsrc = wp_get_attachment_image_src($thumbid);
    $src = $imgsrc[0];
    //   wp_die(var_dump($src)); // debugging/confirmation
}

I don't know how your theme is written or how that meta tag is inserted so I can't say exactly how to insert the image source into that tag, but the above will get you the URL.

$src will be NULL if there is no featured image so use isset or empty to check the variable before using it.

share|improve this answer

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.