如何使_POST_THMBIAN()检索到的特色图像具有响应性?

时间:2016-05-14 作者:orandanow

我试图使循环中的\\u post\\u thumbnai()检索到的特色图像具有响应性,但似乎找不到简化的解决方案。

我想要实现的逻辑如下:

If the screen size is less or equal to 728px
the_post_thumbnai(\'thumbnail\') //Display the thumbnail size image 
Else 
the_post_thumbnai() //display the original size image
有人能建议我如何在没有任何插件的情况下做到这一点吗?

我不是php专家,所以请不要太过拘泥于行话。提前谢谢。

2 个回复
最合适的回答,由SO网友:majick 整理而成

您不能以这种方式组合PHP和CSS,因为PHP是服务器端的,不知道客户端屏幕宽度,而CSS是客户端的,不知道服务器功能。相反,您可以输出两种图像大小,然后使用CSS显示/隐藏它们。例如:

<div class="image-thumbnail"><?php the_post_thumbnail(\'thumbnail\'); ?></div>
<div class="image-fullsize"><?php the_post_thumbnail(\'full\'); ?></div>
和CSS:

@media only screen and (max-width: 728px) {
    .image-thumbnail {display: block;}
    .image-fullsize {display: none;}
}
@media only screen and (min-width: 729px) {
    .image-thumbnail {display: none;}
    .image-fullsize {display: block;}
}
另一种方法是输出一幅全尺寸图像,并使用CSS根据客户端屏幕宽度调整宽度。

<div class="image-fullsize"><?php the_post_thumbnail(\'full\'); ?></div>
使用CSS:

@media only screen and (max-width: 728px) {
   .image-fullsize img {width: 200px; height:auto;}
}
@media only screen and (min-width: 729px) {
   .image-fullsize img {width: 100%; height:auto;}
}
不同之处在于,第一种方法将显示裁剪后的缩略图,而第二种方法将为缩略图显示缩放后的全尺寸图像(稍后可以添加更多断点)

SO网友:BillK

生成HTML输出的PHP模板不知道屏幕大小。您可以使用img srcset 但您不能完全兼容浏览器。或者,只需使用CSS来调整图像大小。

相关推荐

Wp_List_Table not responsive

im使用wp_list_table 在我的后端主题上初始化,以显示我保存在数据库中的一些信息,但有很多信息,我想让它响应,根据屏幕大小显示/隐藏一些元素。我以为它会自动发生,就像在显示帖子的表中一样,但事实并非如此。以下是小屏幕中所有帖子的示例这是我在小屏幕上的wp\\u list\\u表我如何在没有黑客的情况下修复这个bug,wordpress core有什么解决方案吗?类似帖子谢谢