最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Image not showing in an archive page

programmeradmin3浏览0评论

Below each custom post I have two images and a title and excerpt of other posts to read, but the images aren't loading. If I look at the code I see that the image variable is empty.

<?php
    foreach( $recent_posts as $recent_post ) {
        setup_postdata( $recent_post );
        $image_id   = get_post_thumbnail_id( $recent_post->ID );
        $image_url  = '';
        if( $image_id > 0 ) {
            $image_url    = wp_get_attachment_url( $image_id, 'medium' );
        }

?>

Should the $image_url be empty?

Below each custom post I have two images and a title and excerpt of other posts to read, but the images aren't loading. If I look at the code I see that the image variable is empty.

<?php
    foreach( $recent_posts as $recent_post ) {
        setup_postdata( $recent_post );
        $image_id   = get_post_thumbnail_id( $recent_post->ID );
        $image_url  = '';
        if( $image_id > 0 ) {
            $image_url    = wp_get_attachment_url( $image_id, 'medium' );
        }

?>

Should the $image_url be empty?

Share Improve this question edited Apr 10, 2020 at 12:59 Tofandel 2362 silver badges9 bronze badges asked Apr 10, 2020 at 5:11 HenryHenry 9831 gold badge8 silver badges31 bronze badges 3
  • 1 I'm not a php programmer but in the code above you are setting image_url to empty, which is fine but then there is an if clause. Is the if clause satisfied so statement after it is executed? – Admiral Noisy Bottom Commented Apr 10, 2020 at 5:31
  • The problem is not in the code you posted.. Is the image correctly printed in html..? – Tofandel Commented Apr 10, 2020 at 7:40
  • I agree, the problem could be with the conditional check, or anywhere else in your (unposted) code... – Michael Commented Apr 10, 2020 at 17:16
Add a comment  | 

1 Answer 1

Reset to default 1

Most of this code is unnecessary. Remove everything inside the foreach from your question and just use:

foreach ( $recent_posts as $recent_post ) {
    $image = get_the_post_thumbnail( $recent_post, 'medium' );
    // etc.
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论