2012-04-27 64 views
0

如果我打电话这样的帖子,每篇文章包含一些图像,是否有任何PHP我可以用来找到每篇文章中的所有图像的总宽度,然后将div id =“page”设置为最大的宽度?WordPress的/ PHP的 - 设置div宽度,以适应所有图像

<div id="page"> 
     <?php if (have_posts()) while (have_posts()) : the_post(); ?> 
      <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>> 
       <div class="entry-content"> 
        <?php the_content(); ?> 
       </div><!-- .entry-content --> 
      </div><!-- #post-## --> 
    </div> 

在javascript中很容易,我是PHP新手,但怀疑它是可行的? 干杯!

+0

你是否将图像的宽度存储在数据库中? – Phphelp 2012-04-27 00:46:17

+0

getImageSize()可能适合你。 http://us3.php.net/manual/en/function.getimagesize.php – 2012-04-27 00:52:02

+0

这似乎有点落后于我......为什么不尝试将图像限制为适合您的内容div的设置大小?一行css应该做的伎俩。另外,是的,使用js很容易。 – 2012-04-27 02:07:48

回答

0

php运行在服务器端,这意味着当服务器生成页面时,它不知道页面上图像的大小。

您可以用函数

get_the_content()页面

内容,然后使用功能

preg_match_all

得到src和宽度的图像。

然后,如果图片没有属性宽度,请使用getimagesize()和src参数来获取宽度。 然后计算div id =“page”的大小,但这种方式会使服务器超载。最好在客户端使用js。

相关问题