我正在尝试将帖子标题添加为图片的标题标签(实际上是自定义字段)。 我尝试使用each()函数没有成功,但也许解决方案将来自PHP? 这是我尝试过的事情之一:
$('.front a img').each(function(){
var frontvalue = '<?php the_title_attribute(); ?>';
$(this).attr('title',frontvalue);
});
提前致谢!
答案 0 :(得分:1)
在循环内你可以做到:
<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
?>
或者甚至更好,如果它是旧的自定义字段,则提取网址,但不要触摸新类型的自定义字段(只是网址)
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标签。如果旧格式不同于以下内容,您可能需要阅读explode():
<img src="xxxxxx">