在 Wordpress 中使用 the_post_thumbnail() 时特色图像的位置不正确


Incorrect positioning of the featured image when using the_post_thumbnail() in Wordpress

我在这方面有点

新手,但我正在尝试创建一个wordpress函数,用于在带有简码的页面上显示最新4篇文章的片段。这个想法是有 4 个面板,每个面板显示帖子标题、特色图片和摘录。我可以让所有这些元素出现在页面上。但是,图像始终插入到其余内容的上方。该函数的代码为:

function recent_posts_function($atts){
    extract(shortcode_atts(array(
    'posts' => 1,
    ), $atts));
    $return_string = '<h2>Recent Projects</h2><div>';
    query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
    if (have_posts()) :
        while (have_posts()) : the_post();
            $return_string .= '<div class="portfolio-posts"><h4><a href="'.get_permalink().'">'.get_the_title().'</a></h4>'.get_the_excerpt().'<a href="'.get_permalink().'" title="'.get_the_title().'">'.the_post_thumbnail('medium').'</a></div>';
        endwhile;
    endif;
    $return_string .= '</div>';
    wp_reset_query();
    return $return_string;
}

此重新调整的代码将图像放在首位,其余内容如下所示:

<div class="entry-content">
    <img src="http://URL.com/image1.jpg" class="attachment-medium wp-post-image" alt="alt text 1">
    <img src="http://URL.com/image2.jpg" class="attachment-medium wp-post-image" alt="alt text 2">
    <h2>Recent Posts</h2>
    <div>
        <div class="portfolio-posts">
            <h4><a href="http://www.URL.com/category/post-title-1/">Post Title 1</a></h4>
            <p class="lead">excerpt text from post goes here</p>
            <p class="more-link-p"><a class="btn btn-danger" href="http://www.URL.com/category/post-title-1/">Read more →</a></p>
            <a href="http://www.URL.com/category/post-title-1/" title="Post Title 1"></a>
        </div>
        <div class="portfolio-posts">
            <h4><a href="http://www.URL.com/category/post-title-2/">Post Title 2</a></h4>
            <p class="lead">excerpt text from post 2 goes here</p>
            <p class="more-link-p"><a class="btn btn-danger" href="http://www.URL.com/category/post-title-2/">Read more →</a></p>
            <a href="http://www.URL.com/category/post-title-2/" title="Post Title 2"></a>
        </div>
    </div>
</div>

问题是如何让图像显示在标题下方和摘录文本之前?任何帮助将不胜感激!

谢谢。。。

所以,我记得不久前我自己也遇到了这个问题。事实证明the_post_thumbnail是请求内容的"转储"。您需要的是可以回显或存储在变量中以供以后使用的东西。

解决方案是get_the_post_thumbnail - 查看链接

由此,您将能够将其echo或存储在变量中以供以后使用,而不是像某种野蛮人一样被严厉地倾倒。

祝您编码愉快!