duanlisha2335 2013-03-25 14:47
浏览 69
已采纳

如何在循环中为图像添加不同的“标题”标签?

I'm trying to add the post title as a title tag to an image ( that is actually a custom field). I tried using each() function with no success, but maybe the solution will come from php ? This is one of the things i tried:

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

Thanks in advance !

  • 写回答

1条回答 默认 最新

  • doutong1890 2013-03-25 15:03
    关注

    Inside the loop you can do:

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

    or with custom field:

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

    In your specific case, you could remove the last closing bracket and then append the alt property:

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

    Or even better, extract the url if it's an old type custom field, but don't touch the new type custom fields (which are just the 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
    }
    

    and then rebuild your img tag from there. You might wanna read up on explode() if the old format is different from:

    <img src="xxxxxx">
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#网络安全#的问题:求ensp的网络安全,不要步骤要完成版文件
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥20 使用Photon PUN2解决游戏得分同步的问题
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM