0

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.

2

2 Answers 2

1

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.

2
  • 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, 2013 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, 2013 at 13:12
0

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'));
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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