【问题标题】:Wordpress / PHP - Set div width to fit all imagesWordpress / PHP - 设置 div 宽度以适合所有图像
【发布时间】:2012-05-07 18:27:43
【问题描述】:

如果我这样调用帖子,每个帖子都包含许多图像,是否有任何 PHP 可以用来查找每个帖子中所有图像的总宽度,然后将 div id="page" 设置为最厉害的?

    <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 新手,但怀疑它是可行的? 干杯!

【问题讨论】:

  • 你是把图片的宽度存储在数据库里还是什么的?
  • getImageSize() 可能对你有用。 us3.php.net/manual/en/function.getimagesize.php
  • 这对我来说似乎有点倒退...为什么不尝试将图像限制为适合您的内容 div 的设置大小?一行css应该可以解决问题。另外,是的,使用 js 很容易做到这一点。

标签: php image wordpress html width


【解决方案1】:

php运行在服务器端,也就是说服务器在生成页面时,并不知道页面上图片的大小。

可以带功能

get_the_content()

页面内容,然后使用函数

preg_match_all

获取图片的src和宽度。

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

【讨论】:

    猜你喜欢
    • 2013-01-03
    • 2013-02-17
    • 1970-01-01
    • 2021-12-28
    • 1970-01-01
    • 2014-04-11
    • 2013-02-25
    • 2014-02-21
    • 1970-01-01
    相关资源
    最近更新 更多