如何在Wordpress中使用PHP更改HTML标记和属性?

如何在Wordpress中使用PHP更改HTML标记和属性?,php,html,image,wordpress,Php,Html,Image,Wordpress,在我的wordpress博客中,我试图设置一个code/a函数,使用phpThumbnail库将文章中的每个图像都转换为缩略图 到目前为止,我已经有了这个函数,它在帖子中生成一个图像数组,并使用\u thumboil\u id元键从第一个图像创建一个缩略图 function sThumbnail() { $post_id = get_the_ID(); $post = get_post($post_id); $first_img = ''; ob_start(); ob_end_clean();

在我的wordpress博客中,我试图设置一个code/a函数,使用
phpThumbnail
库将文章中的每个图像都转换为缩略图

到目前为止,我已经有了这个函数,它在帖子中生成一个图像数组,并使用
\u thumboil\u id
元键从第一个图像创建一个缩略图

function sThumbnail() {
$post_id = get_the_ID();
$post = get_post($post_id);
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches[1][0];

var_dump($matches);

$first_img = substr($first_img, strlen(site_url()));

$post_thumbnail = get_post_meta( $post_id, '_thumbnail_id', true );

if (!wp_is_post_revision($post_id)) { // Verify that post is not a revision
    if (empty($post_thumbnail)) {       // Check if Thumbnail does NOT exist!
          if(empty($first_img)){
            update_post_meta( $post_id, '_thumbnail_id', 'http://static.guim.co.uk/sys-images/Guardian/About/General/2012/11/1/1351779022327/Nicki-Minaj---You-ve-neve-010.jpg' );
          } else {
              update_post_meta( $post_id, '_thumbnail_id', $first_img );
          } // Get the first image of a post (if available)
    }
}
}
函数sThumbnail(){
$post_id=获取_id();
$post=get\u post($post\u id);
$first_img='';
ob_start();
ob_end_clean();
$output=preg_match_all('//i',$post->post_content,$matches);
$first_img=$matches[1][0];
var_dump($matches);
$first\u img=substr($first\u img,strlen(site\u url());
$post\u thumboil=get\u post\u meta($post\u id,'u thumboil\u id',true);
如果(!wp_是发布修订($post_id)){//请验证发布不是修订
if(空($post_缩略图)){//检查缩略图是否不存在!
如果(空($first_img)){
更新发布元($post\u id,''u缩略图\u id','http://static.guim.co.uk/sys-images/Guardian/About/General/2012/11/1/1351779022327/Nicki-Minaj---You-ve-neve-010.jpg' );
}否则{
更新发布元($post\u id,''u缩略图\u id',$first\u img);
}//获取文章的第一个图像(如果可用)
}
}
}
经过一些修改,我可以使用该功能获取帖子中的所有图像,然后对它们进行处理。
问题是,我不知道如何更改
属性的
src
属性,也不知道如何更改/使用标记及其属性的一般信息

一些要点:
-为了澄清,我所说的
src
,是指

-上面的函数不是我要使用的函数

它仅供参考,因为它的前6/7行是获取图像标记的
src
属性内容的方法

通过调用
ob\u start
,然后立即调用
ob\u end\u clean
,您可能希望实现什么?我不这么认为。我发布的函数仅供参考,表明我知道如何获取图像的
src
属性,但不知道如何更改它。这正是我要问的。Madbreaks只是告诉你你的代码做了一些荒谬的事情。它打开“输出缓冲区”(ob_start),然后立即清除并关闭它。无论你是否正在使用它。。。这仍然是荒谬的。同样荒谬的是,在$post=get_post($post_ID);,后面加上get_ID()(这意味着你已经有了“post”;。。。这个代码完全不连贯。你在哪里找到的?哦,对不起!我可能误读了他的评论。。。这是我从这两个链接中提供的代码:和<代码>获取ID()的部分实际上是我的错。谢谢你的提醒!我马上修好!顺便说一下,我使用了
publish\u post
wordpress挂钩,它返回一个ID,而不是post。因此,它要求我使用
get\u ID()
,即使我现在知道我应该将函数设置为接收
ID
变量(
sThumbnail(
post\u ID)