Skip to content

Instantly share code, notes, and snippets.

@madysondesigns
Created April 26, 2012 14:30
Show Gist options
  • Save madysondesigns/2499962 to your computer and use it in GitHub Desktop.
Save madysondesigns/2499962 to your computer and use it in GitHub Desktop.
Opengraph thumbnails
In WP functions.php:
function get_post_id() { global $wp_query; $id = $wp_query->post->ID; return $id; }
function opengraph_thumbnail(){
if (is_single()) {
echo get_the_post_thumbnail(get_post_id());
} else {
echo imagedir() ?>/about_better.png <?php ;
}
}
In WP header.php:
<meta property="og:image" content="<?php opengraph_thumbnail(); ?>" />
Output is this:
<meta property="og:image" content="" />
:(
@shellycole
Copy link

Try this:

function get_post_id() { 
    global $wp_query; 
    $id = $wp_query->post->ID; 
    return $id; 
}

function opengraph_thumbnail(){
        $id = get_post_id();
    if (is_single() && has_post_thumbnail($id) !== FALSE) { 
        $image = wp_get_attachment_image_src( get_post_thumbnail_id( $id ), 'single-post-thumbnail' );
        echo $image[0];
    } else {
        echo imagedir() ?>/about_better.png <?php ;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment