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

2019-05-10 23:09发布

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!

标签: php wordpress
1条回答
做自己的国王
2楼-- · 2019-05-10 23:18

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' );
}
查看更多
登录 后发表回答