如何添加不同的“标题”标记到循环中的图像


How to add different "title" tags to images within the loop?

我试图将帖子标题作为标题标签添加到图像(这实际上是一个自定义字段)。我尝试使用each()函数没有成功,但也许解决方案将来自php ?这是我尝试过的方法之一:

$('.front a img').each(function(){
var frontvalue = '<?php the_title_attribute(); ?>';
$(this).attr('title',frontvalue);
});

在循环中你可以这样做:

<img src="image.jpg" alt="<?php echo get_the_title();?>">

或自定义字段:

<img src="<?php echo get_post_meta(get_the_ID(), "yourCustomFieldName", true);?>" alt="<?php the_title();?>">

在您的特定情况下,您可以删除最后一个右括号,然后附加alt属性:

<?php
$From = substr(trim($From),0,-1);        //cut the last char
$From .= ' alt="'.get_the_title().'">';  //append the title
?>

或者更好,如果它是一个旧类型的自定义字段,提取url,但不触及新类型的自定义字段(只是url)

if(substr(trim($From),0,5) == '<img '){  //is this an img tag i see?
  $boom = explode('"', $From, 3);        //it is, aim for the quotes
  $From = $boom[1];                      //salvage the url
}

,然后从那里重新构建img标记。如果旧格式与:

不同,你可能需要阅读一下explosion ()
<img src="xxxxxx">