最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Unable to display the post thumbnail in the loop

matteradmin9PV0评论

I am trying to create a blog-listing page, that'll display the title and the featured images. Here's the loop that I am using

page.php

<div class="p-5 container" style="text-align:center;">
    <h1 style="margin-bottom:5vh;font-weight:bold;">Latest Posts</h1>
    <section class="thumbnails row">
    <?php $wpdb = new WP_Query(array(
            'post_type'=>'post',
            // 'post_type'       => 'publish',
            'post_status' => 'any',
            'posts_per_page' => 10));

            if($wpdb->have_posts()):
                while($wpdb->have_posts()):
                    $wpdb->the_post();?>
            <?php
            get_template_part('blog-show');
                // echo the_title();
                endwhile;
            endif;
            ?>
    </section>
</div>

blog-show.php

<div class="col-12 col-sm-4">
    <a href="<?php echo the_permalink() ?>">
        <img src="<?php the_post_thumbnail(); ?>"
             alt="<?php the_post_thumbnail_caption() ?>"/>
        <h3><?php the_title(); ?></h3>
    </a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</div>

I am able to use the the_post_thumbnail in another page but it doesn't work here.

I am trying to create a blog-listing page, that'll display the title and the featured images. Here's the loop that I am using

page.php

<div class="p-5 container" style="text-align:center;">
    <h1 style="margin-bottom:5vh;font-weight:bold;">Latest Posts</h1>
    <section class="thumbnails row">
    <?php $wpdb = new WP_Query(array(
            'post_type'=>'post',
            // 'post_type'       => 'publish',
            'post_status' => 'any',
            'posts_per_page' => 10));

            if($wpdb->have_posts()):
                while($wpdb->have_posts()):
                    $wpdb->the_post();?>
            <?php
            get_template_part('blog-show');
                // echo the_title();
                endwhile;
            endif;
            ?>
    </section>
</div>

blog-show.php

<div class="col-12 col-sm-4">
    <a href="<?php echo the_permalink() ?>">
        <img src="<?php the_post_thumbnail(); ?>"
             alt="<?php the_post_thumbnail_caption() ?>"/>
        <h3><?php the_title(); ?></h3>
    </a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</div>

I am able to use the the_post_thumbnail in another page but it doesn't work here.

Share Improve this question asked Jan 2, 2019 at 8:09 Sahil ShuklaSahil Shukla 36 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

the_post_thumbnail displays the post thumbnail. It generates HTML tag and echoes it. It does not echo only the URL to that image.

So this line:

<img src="<?php the_post_thumbnail(); ?>"
         alt="<?php the_post_thumbnail_caption() ?>"/>

Generates something like this:

<img src="<img src="" ... />"
         alt="..."/>

So it's not a correct HTML.

I'd changed it to:

<div class="col-12 col-sm-4">
    <a href="<?php the_permalink(); ?>">
        <?php the_post_thumbnail( 'post-thumbnail', array( 'alt' => get_the_post_thumbnail_caption() ) ); ?>
        <h3><?php the_title(); ?></h3>
    </a>
</div>

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far