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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 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.
Method 2
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'));
?>
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0