【问题标题】:Wordpress how to get the post thumbnail inside a figure tagWordpress如何获取图形标签内的帖子缩略图
【发布时间】:2021-10-30 09:17:46
【问题描述】:

我正在运行一个查询循环,并希望“if”帖子具有特色图片,它需要位于 <figure> 标记内。但是特色图片的<img> 标签出现在<figure> 标签之前。可能是什么原因?

        foreach($posts as $post){
            setup_postdata($post);
            

            echo '<article class="pet_post">';
                if(has_post_thumbnail()) { echo '<figure class="post_thumbnail">'. the_post_thumbnail('full') . '</figure>'; };
                echo '<h1 class="post_title">'. get_the_title() . '</h1>';
                echo '<p class="pet_seller">'. get_the_content() . '</p>';
                echo '<form method="POST">';
                    echo '<input type="hidden" name="post-id" value="'. get_the_ID() . '">';
                    echo '<button type="submit" name="delete-post">Delete this post</button>';
                echo '</form>';   
            echo '</article>';
        };

用这张图可以更好地理解这个问题:

【问题讨论】:

  • 不,因为使用 the_post_thumbnail('full') 的结果是 Wordpress 插入了 img 标签。无论如何,img 标签都不会像 那样使用右斜杠。

标签: php wordpress wordpress-theming thumbnails custom-wordpress-pages


【解决方案1】:

这是因为您使用的是the_post_thumbnail 函数,默认情况下会回显图像,这比回显figure 标记要早。

替换

echo '<figure class="post_thumbnail">'. the_post_thumbnail('full') . '</figure>';

echo '<figure class="post_thumbnail"><img src="' . get_the_post_thumbnail_url(get_the_ID(), "full") . '"></figure>';

【讨论】:

  • 非常感谢您的澄清。聪明的解决方案。 :D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-15
  • 1970-01-01
  • 2014-03-12
  • 1970-01-01
  • 1970-01-01
  • 2021-04-06
  • 1970-01-01
相关资源
最近更新 更多