簡體   English   中英

PHP / Wordpress-獲取的寬度/高度屬性<img>

[英]PHP/Wordpress - Get the width/height attributes of a <img>

我目前正在使用基於Wordpress的網站的新版本,並且有一個我想在不知道如何添加的功能。

所以也許您可以幫助我...我希望如此!

我想在the_content上添加一個過濾器,該過濾器適用於其中的每個<img ...> 此函數將在變量( $thisWidth$thisHeight )獲取width和height屬性(例如<img src="image.jpg" **height="120" width="300**" alt="test" /> ) )。

然后,我將比較每個圖像的寬度和高度,以了解它是橫向格式還是縱向格式,並添加一個相應的類。 容易: if($width > $height) { ... } else { ... }

我不習慣使用regex ,所以這就是為什么我需要您的幫助。 我的技能使我能夠編寫函數的基礎,但僅此而已:

function imgClass($content) {

   /* for each '<img ....>' Loop ? */
   $thisWidth = /* get html width attribute */;
   $thisHeight = /* get html height attribute */;
   if($width > $height) { /* add class='landscape' */ } else { /* add class='portrait' */ }
   return $content;
}
add_filter('the_content','imgClass');

我非常感謝任何能夠幫助我的人……預先感謝!

這是一個使用javascript的好例子

    <!DOCTYPE html>
<html>
<body>

<img id="planets" src="planets.gif" width="145" height="126" usemap="#planetmap">



<p>Click the button to display the coordinates for the "planets.gif" area.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
    var x = document.getElementById("planets").width;
    var y = document.getElementById("planets").height;
}
</script>

</body>
</html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM