Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Get current post featured image outside loop WordPress

  • SOLVED


Hello,

I am creating so og:meta properties, but I am trying to retrieve the featured image from the current post, but outside the loop.

Can anyone help me to get this to work?



<meta property="og:image" content="<?php

$ogFacebook = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'facebook-thumb' );

if ( has_post_thumbnail() ) {

echo $ogFacebook[0];

} else {

echo get_bloginfo('template_url') . '/images/facebook-default.jpg';

} ?>" />


If this helps, I am using this globally...


global $currentID;
$currentID = get_the_ID();


Currently my og:image is always displaying the facebook-default.jpg but my posts definitely have 'facebook-thumb' featured images.

Thanks

Answers (7)

2012-07-16

Ivaylo Draganov answers:

I think you need to call the global $post var first:

global $post;


Josh Cranwell comments:

Still just returning default image?? Weird?


<meta property="og:image" content="<?php

global $post;

$ogFacebook = wp_get_attachment_image_src( get_post_thumbnail_id( $currentID ), 'thumbnail' );

if ( has_post_thumbnail() ) {

echo $ogFacebook[0];

} else {

echo get_bloginfo('template_url') . '/images/facebook-default.jpg';

} ?>" />


Ivaylo Draganov comments:

Try this:

<meta property="og:image" content="<?php
global $post;

$ogFacebook = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'thumbnail' );
if ( has_post_thumbnail( $post->ID ) ) {
echo $ogFacebook[0];
} else {
echo get_bloginfo('template_url') . '/images/facebook-default.jpg';
} ?>" />


You need to pass a post ID to all of the functions (has_post_thumbnail and get_post_thumbnail_id).


Josh Cranwell comments:

Thanks you answer worked the whole time it was me being an idiot.

2012-07-16

Hai Bui answers:

Please try this:
<meta property="og:image" content="<?php
global $post;
$ogFacebook = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'facebook-thumb' );
if ( $ogFacebook ) {
echo $ogFacebook[0];
} else {
echo get_bloginfo('template_url') . '/images/facebook-default.jpg';
} ?>" />


Hai Bui comments:

I think "global $post;" is not needed, try this
<meta property="og:image" content="<?php

$ogFacebook = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'facebook-thumb' );

if ( $ogFacebook ) {

echo $ogFacebook[0];

} else {

echo get_bloginfo('template_url') . '/images/facebook-default.jpg';

} ?>" />


Josh Cranwell comments:

Hi Hai,

This is so weird...

I tried this, I thought it was something to with my facebook-thumb. But it definitely exists. But I tested it with the standard thumbnail and it still returns the default image url.


<meta property="og:image" content="<?php

global $post;
$ogFacebook = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'thumbnail' );

if ( $ogFacebook ) {

echo $ogFacebook[0];

} else {

echo get_bloginfo('template_url') . '/images/facebook-default.jpg';

} ?>" />



How can this happen? Can you test?


Josh Cranwell comments:

nope still returning default?


Hai Bui comments:

It works for me. What kind of page are you using to test this? Can you give me a url?


Josh Cranwell comments:

Thanks your answer worked the whole time it was me being an idiot. Didnt have featured image set but thought I did :/

2012-07-16

Arnav Joy answers:

try this


<?php wp_reset_query(); ?>
<meta property="og:image" content="<?php



$ogFacebook = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'facebook-thumb' );



if ( !empty($ogFacebook) ) {



echo $ogFacebook[0];



} else {



echo get_bloginfo('template_url') . '/images/facebook-default.jpg';



} ?>" />


Josh Cranwell comments:

Thanks, but this still just returns default facebook image.

I've even tested it with the normal thumbnail - does this work for you?


<meta property="og:image" content="<?php

$ogFacebook = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'thumbnail' );

if ( !empty($ogFacebook) ) {

echo $ogFacebook[0];

} else {

echo get_bloginfo('template_url') . '/images/facebook-default.jpg';

} ?>" />


Arnav Joy comments:

can you tell me in which file are you using your code?

single.php or page.php or .....


Josh Cranwell comments:

Thanks you answer worked the whole time!!! :/

2012-07-16

Romel Apuya answers:

use the $currentID in getting the post thumbnail


<meta property="og:image" content="<?php
$ogFacebook = wp_get_attachment_image_src( get_post_thumbnail_id( $currentID ), 'facebook-thumb' );
if ( has_post_thumbnail() ) {
echo $ogFacebook[0];
} else {
echo get_bloginfo('template_url') . '/images/facebook-default.jpg';

} ?>" />


Josh Cranwell comments:

Yes this worked thanks!!!

2012-07-16

Francisco Javier Carazo Gil answers:

Try this:
// Works inside of the Loop
function function_name() {
global $post;
$thePostID = $post->ID;
}

or:


// Works in single post outside of the Loop
function function_name() {
global $wp_query;
$thePostID = $wp_query->post->ID;
}


Josh Cranwell comments:

Thanks I will use this again

2012-07-16

Nilesh shiragave answers:

If you are checking outside loop then you must pass post ID to has_post_thumbnail() function.

Please check by

if(has_post_thumbnail($post->ID))
{
}
else
{
}

2012-07-16

Sabby Sam answers:

Hi,
Your code never fetch the thumbnail image if it is exist also.

First tell me where you are exactly fetching this code.

If you are fetching this code on single.php then you need to do this

<?php while ( have_posts() ) : the_post(); ?>

<meta property="og:image" content="<?php



$ogFacebook = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'facebook-thumb' );



if ( has_post_thumbnail() ) {



echo $ogFacebook[0];



} else {



echo get_bloginfo('template_url') . '/images/facebook-default.jpg';



} ?>" />

<?php endwhile; // end of the loop. ?>

then only it will work if you want to fetch this outside the loop then you need to store in a variable.

Please specify the url of the site, i will help you.