2013-07-08 53 views
0

我想用post标题替换使用jQuery的WordPress帖子/页面中存在的所有图像的alt和标题属性。更改/添加图像标题和ALT内的WordPress帖子

我尝试了几个代码,但没有任何工作可以帮助我吗?

这是我试过的代码之一。看起来它应该工作,但事实并非如此。

<?php function ia_prop_js() { 
global $post; ?> 
<script type="text/javascript"> 
$(document).ready(function() { 
    $(".hentry").find("img").each(function() { 
      $(this).attr("title", "<?php echo esc_attr(get_the_title($post->ID)); ?>"); 
      $(this).attr("alt", "<?php echo esc_attr(get_the_title($post->ID)); ?>"); 
    }); 
}); 
</script> 
<?php } 
add_action('wp_head', 'ia_prop_js'); 
?> 
+0

有什么问题?你有没有尝试修改标题/ ALT与静态文本和发生了什么?你有没有尝试用'$(“。hentry img”)'选择图像? –

+0

是的,我试过了。我甚至尝试过jQuery无冲突模式代码。 – Abhik

+0

我做了一个简化[小提琴](http://jsfiddle.net/nd84e/)似乎工作正常。要么提供一个网址,要么修改此提琴以更好地帮助您 –

回答

1

在您的测试网址,如果我这个控制台上

jQuery(".hentry").find("img").each(function() { 
     jQuery(this).attr("title", "new"); 
     jQuery(this).attr("alt", "new"); 
}); 

它改变标题和替代。因此,无论它是$或您get_the_title不返回标题(也许是外循环)

+0

就是这样。只要我把它挂在wp_footer中,这一块就可以工作。 :) 谢了哥们。 – Abhik

0

这是为我工作:

$(document).ready(function() { 
    $('img.hentry').each(function() { 
      $(this).attr("title", "bbbbbbbbbbbbbb"); 
      $(this).attr("alt", "xxxxxxxxxxxx"); 
    }); 
}); 

也请看看这里:http://jsfiddle.net/4qgaX/

+0

你的jquery选择器是错误的 –

+0

你知道这一切错了队友。这个图像是在一个带有hentry类的div里面。 – Abhik

0

使用这两个功能,这些都将有很大的帮助:这两个被测试

/** 
* Rename the Media/image filename to Post title 
* Example: title I love icecream - filename: i-love-icecream-23.jpg 
* 
*/ 

function wpsx_5505_modify_uploaded_file_names($arr) { 

    // Get the parent post ID, if there is one 
    if(isset($_REQUEST['post_id'])) { 
     $post_id = $_REQUEST['post_id']; 
    } else { 
     $post_id = false; 
    } 

    // Only do this if we got the post ID--otherwise they're probably in 
    // the media section rather than uploading an image from a post. 
    if($post_id && is_numeric($post_id)) { 

      // Get the post title 
     $post_title = get_the_title($post_id); 

      // Get the post slug 
     $post_slug=preg_replace('/[^A-Za-z0-9-]+/', '-', $post_title); 

     // If we found a slug 
     if($post_slug) { 

      $random_number = rand(10000,99999); 
      $arr['name'] = $post_slug . '-' . $random_number . '.jpg'; 

     } 

    } 

    return $arr; 

} 
add_filter('wp_handle_upload_prefilter', 'wpsx_5505_modify_uploaded_file_names', 1, 1); 

要添加的文件名以标题和标题,使用下面的代码

/** 
* Add the Media/Image filename to caption, Title 
* 
*/ 
function wpsx_5505_modify_uploaded_file_meta($meta, $file, $sourceImageType) { 

    // Get the parent post ID, if there is one 
    if(isset($_REQUEST['post_id'])) { 
     $post_id = $_REQUEST['post_id']; 
    } else { 
     $post_id = false; 
    } 

    // Only do this if we got the post ID--otherwise they're probably in 
    // the media section rather than uploading an image from a post. 
    if($post_id && is_numeric($post_id)) { 

     // Get the post title 
     $post_title = get_the_title($post_id); 

     // If we found a title 
     if($post_title) { 

      $meta['title'] = $post_title; 
      $meta['caption'] = $post_title; 


     } 

    } 

    return $meta; 

} 
add_filter('wp_read_image_metadata', 'wpsx_5505_modify_uploaded_file_meta', 1, 3); 
相关问题