Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get featured image of a page (no post) in Wordpress

Tags:

php

wordpress

I need to show the featured images of all the pages, not the posts. I have this code:

<?php
if ((is_singular() || is_home()) && current_theme_supports('post-thumbnails')) : echo get_the_post_thumbnail( '12', 'full' ); ?>
<img src="<?php header_image(); ?>" class="header-img" alt="" />
<?php endif;?>

But this only shows one featured image.

Thank you so much!

like image 666
krathos Avatar asked Nov 29 '25 15:11

krathos


1 Answers

You can simply use WP_Query to get that,

$loop = new WP_Query( array( 'post_type' => 'page', 'meta_key' => '_thumbnail_id' ) );

Or if you want to do by your way you need to fetch all pages first & than loop over it to get thier feature image,

$args = array(
    'post_type' => 'page',
    'post_status' => 'publish'
); 
$pages = get_pages($args); 
foreach($pages as $page) {
        echo get_the_post_thumbnail( $page->ID, 'full' );
}
like image 183
Rikesh Avatar answered Dec 02 '25 06:12

Rikesh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!