2013-04-20 65 views
1

我现在停留在此场景中。获取特定Div的宽度,然后显示以下代码

我有3个帖子缩略图不同尺寸

根据用户的屏幕大小,我只需要显示一个图像。我不喜欢使用CSS媒体查询或通过CSS调整相同的图像,因为它变得扭曲。无论如何,它是否有可能实施使用PHP IF

是这样的:

IF博客帖子宽度== 360像素 显示<?php the_post_thumbnail('post-thumbnails2200px', array('id' => "FeatureImage")); ?>

否则如果.blogpost宽度== 300像素 显示<?php the_post_thumbnail('post-thumbnails1280px', array('id' => "FeatureImage")); ?>

否则如果.blogpost宽度== 250像素 显示<?php the_post_thumbnail('post-thumbnails600px', array('id' => "FeatureImage")); ?>

这是html结构:

<div class="blogpost" id="blogpost"> 

<a class="post_title" href="<?php the_permalink() ?>" rel="bookmark" alt="<?php the_title(); ?>" title="<?php the_title(); ?>">    
<?php the_post_thumbnail('post-thumbnails2200px', array('id' => "FeatureImage")); ?> 
</a> 

</div> 
+0

需要Javascript获得浏览器的尺寸。您可以在AJAX发送用户的视口大小后加载内容,但该解决方案闻到 – 2013-04-20 11:56:31

+1

,您可以使用GET参数发送缩略图'width'并使用它。 – mehdi 2013-04-20 11:57:09

+0

如果分辨率改变,图像不会随着它改变(浏览器的大小调整) – 2013-04-20 13:06:20

回答

0

,你可以尝试这样的事情 (请注意,如果你有漂亮的永久链接启用确保您使用此行,而不是只是改变了第一&什么?

document.location = \ “$ URL?R = 1 &宽度= \” + screen.width + \ “&身高= \” + screen.height)

<?php  
// Get the current page 
$url = get_permalink($post->ID); 
if(!isset($_GET['r']))  
{  
echo "<script language=\"JavaScript\">  
<!--  
document.location=\"$url&r=1&width=\"+screen.width+\"&Height=\"+screen.height;  
//-->  
</script>";  
}  
else {   

// Code to be displayed if resolutoin is detected  
if(isset($_GET['width']) && isset($_GET['Height'])) {  
      // Resolution detected  
      $blogpost_width = $_GET['width']; 

}  
else {  
      // Resolution not detected  
}  
}  

?> 
相关问题