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.

Is it possible to display a featured image on a news page that uses index.php?

I'm currently using wp_query to display posts from a specific category on the news page. Each post has his own featured image which isn't displayed on the index/news page but only for the post itself.

If i place <?php the_post_thumbnail();? > before the query it displays all featured images for the posts shown on the news page but not the featured image which I've setup for the news page itself.

How can I solve this? If it's even possible as the codex says that the featured image is only for post and pages. The news page is a page but uses index.php instead of page.php.

share|improve this question
    
Have you tried using get_the_post_thumbnail? codex.wordpress.org/Function_Reference/get_the_post_thumbnail –  gdaniel May 2 '13 at 20:34
    
Sorry, used the wrong tag in the question, I'm using the_post_thumbnail indeed. I've edited the question. –  NielsPilon May 2 '13 at 20:42
add comment

2 Answers

up vote 1 down vote accepted

If I am reading your question correctly, the problem is that when you set a page so that it has a an archive Loop on it $wp_query and the $posts variable are set to for the index Loop and not for the page the are on. To get information about that page you need get_queried_object. Two lines will show your featured image.

$obj = get_queried_object();
echo get_the_post_thumbnail($obj->ID);

One line if your PHP is recent enough

echo get_the_post_thumbnail(get_queried_object()->ID);

Obviously you will want to do something a bit more complicated to check for errors and avoid notices, but basically that is it.

Be careful with get_queried_object. It returns very different information depending on the type of page you are on-- index, single, author archive, tab archive, etc.

share|improve this answer
    
Thanks for the reply. I've tried your solution and it shows a featured image. But only the featured image of the first post of the loop and not the featured image of the page itself. I've placed the code before the wp_query. –  NielsPilon May 3 '13 at 6:55
    
Not when I try it. get_queried_object is the page itself. Give that ID to get_the_post_thumbnail and it works. I corrected a typo by the way. Check again. –  s_ha_dum May 3 '13 at 13:12
    
Yes, it works now after the update! Thanks a lot. –  NielsPilon May 3 '13 at 20:37
add comment

In the query, you should use the_post_thumbnail.

<?php the_post_thumbnail( $size, $attr ); ?> 

The size is a keyword or an array, and the attributes are for the image tag.

So, something like

<?php 
the_post_thumbanil('large', array('class' => 'my-class'));
?>

will output:

<img src='img-large.jpeg' class='my-class' />

You can also specify a specific size:

<?php 
the_post_thumbnail(array(100, 100), array('class' => 'my-class'));
?>
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.