get_the_post_thumbnail() 在页面模板中不起作用

get_the_post_thumbnail() not working in a page template

以下代码放在名为 latest-web.php 的包含文件中:

<?php
                    $args = array( 'numberposts' => '8', 'category_name' => 'web-reference' );
                    $recent_posts = wp_get_recent_posts( $args );
                    foreach( $recent_posts as $recent ){
                        $featured_image = get_the_post_thumbnail();
                        $poveznica = get_field('link-projekta');
                        echo '<figure class="effect-winston">
                        ' . $featured_image . '
                        <figcaption>
                            <h2><a href="' . get_permalink($recent["ID"]) . '">' . $recent["post_title"] . '</a></h2>
                            <p>
                                <a href="' . get_permalink($recent["ID"]) . '"><i class="fa fa-fw fa-list"></i></a>
                                <a href="' . $poveznica . '" target="_blank"><i class="fa fa-fw fa-link"></i></a>
                            </p>
                        </figcaption>           
                    </figure>';
                    }
                    wp_reset_query();
?>

将文件包含在index.html 中时,它可以正常工作,但是将其包含在页面模板中时,就会出现问题。该代码正确提取所有信息。但是,image/thumbnail 根本不显示。从浏览器检查时没有 img 标签。有人对此有解决方案吗?

谢谢!

请试试这个,在缩略图功能中添加了最近的id。

<?php
                    $args = array( 'numberposts' => '8');
                    $recent_posts = wp_get_recent_posts( $args );
                    foreach( $recent_posts as $recent ){
                        $featured_image = get_the_post_thumbnail($recent["ID"]);
                        $poveznica = get_field('link-projekta');
                        echo '<figure class="effect-winston">
                        ' . $featured_image . '
                        <figcaption>
                            <h2><a href="' . get_permalink($recent["ID"]) . '">' . $recent["post_title"] . '</a></h2>
                            <p>
                                <a href="' . get_permalink($recent["ID"]) . '"><i class="fa fa-fw fa-list"></i></a>
                                <a href="' . $poveznica . '" target="_blank"><i class="fa fa-fw fa-link"></i></a>
                            </p>
                        </figcaption>           
                    </figure>';
                    }
                    wp_reset_query();
?>

有时 get_the_post_thumbnail() 在您传递 post 的 ID 之前无法工作,所以在您的情况下是 foeach 循环,所以您必须在 [=13= 中传递最近的 [ID] ](), 所以它看起来像 get_the_post_thumbnail(最近的 ['ID']),你完成了伙计