首页 > 解决方案 > 如何捕获特色图像的宽度和高度值 (Php)

问题描述

在 wordpress 中,添加到主题的第一张图片是我的特色图片我将此代码添加到 function.php 出于这个原因

// picture
function catch_that_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
if(preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches)){
$first_img = $matches [1] [0];
return $first_img;
}
else {
$first_img = get_template_directory_uri()."/images/default.jpg";
return $first_img;
}
}
//picture

我在 content.php 中添加了以下代码

<?php if ( has_post_thumbnail()  ) {
          the_post_thumbnail('large');
        } else {
           echo '<img src="';
             echo catch_that_image();
             echo '" alt="'.get_the_title().'" title="'.get_the_title().'"/>';
        }   ?>

但是我的特色图片中没有宽度和高度值(谷歌已经警告过我),我怎样才能自动获取特色图片的宽度和高度值?

标签: php

解决方案


推荐阅读