2013-07-08 51 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); 
相關問題