2012-04-26 354 views
3

我已經創建了一個圖像的WordPress的博客。發佈所有帖子時,我沒有使用wordpress編輯器上傳圖片。相反,我使用FillaZilla上傳圖像。然後,在wordpress編輯器中,我只手動編寫了所有帖子中的圖片標籤(以下)並將其發佈。所有帖子都不包含文字,只包含圖片。與此類似,添加自動超鏈接到圖像

<img alt="" title="" src=""></img> 

現在我想問你的是,我想在所有職位的圖像得到自動超鏈接地址相同圖片src。我在wordpress博客中有超過200篇博文。我不想一一編輯它們。這裏是wordpress內容區域的編碼,

<div class="post-entry"> 
              <p><img src='http://www.mywebsite.com/wp-content/uploads/2012/04/sun.jpg' title="sun" alt="sun" /></p> 

</div> 

任何人都可以請幫助我嗎?我如何添加超鏈接到圖像?有什麼代碼可以放入wordpress主題頁面的post-entry div中嗎?

@ chandu-vkm解釋(在評論中)我正在尋找什麼。現在我還有一個問題。當我在img之前添加span標籤時,提到的代碼@chandu-vkm不允許我在img標籤之前添加span標籤。相反,它將place標籤放置在p標籤之外,如下面的代碼所示。

<div class="post_div"> 
<span class="entry"></span> 
<p> 
<img src='http://www.mywebsite.com/wp-content/uploads/2012/04/sun.jpg' title="Cute Teddy Bear" alt="Cute Teddy Bear" /> 
</p> 
</div> 

但我希望跨度放在p之後,就像這樣。

<div class="post_div"> 
     <p> 
     <span class="entry"></span> 
     <img src='http://www.mywebsite.com/wp-content/uploads/2012/04/sun.jpg' title="Cute Teddy Bear" alt="Cute Teddy Bear" /> 
     </p> 
    </div> 

有人請幫助我。

回答

2

你可以用一些jQuery的做

<div class="post_div"> 
    <img src='http://www.mywebsite.com/wp-content/uploads/2012/04/sun.jpg' title="sun" alt="sun" /> 
</div> 

這樣

$('.post_div img').each(function(){ 
$(this).wrap(function() { 
    return '<a href="' + $(this).attr('src') + '" />'; 
}) 
}); 

這裏的樣品http://jsfiddle.net/a4PYd/

+0

真棒!謝謝@Chandu :) – 2012-04-26 11:00:19

+0

繼續約翰 – 2012-04-26 11:23:57

+0

@ chandu-ckm問題已被編輯。我需要你的幫助:) – 2012-04-26 11:33:48

0

如果你相信,你的所有的文章內容只包含<img>標籤,你可以在這個代碼段添加到您的functions.php文件:

function hyperlink_all_my_content($content) { 
    $link = "http://www.somelink.com"; 
    return "<a href='$link'>$content</a>"; 
} 
add_filter('the_content', 'hyperlink_all_my_content'); 

注意,這將鏈接所有內容,甚至在你的wordpress頁面上。

編輯:

function hyperlink_all_my_content($content) { 

    $matches = array(); 
    $nummatches = preg_match("/src=['|\"](.*)['|\"]/", $content, $matches); 
    return "<a href='" . $matches[1] . "'>$content</a>"; 
} 
add_filter('the_content', 'hyperlink_all_my_content'); 
+0

OP希望數千種不同的$鏈接,每個等於th e'src'的各種圖像。使用'add_filter()'是一個非常好的主意,儘管 – FelipeAls 2012-04-26 10:51:17

+0

我想使用圖像src作爲超鏈接。非常感謝您的幫助。由Chandu發佈的上述方法工作就像一個魅力:) – 2012-04-26 11:02:11

+0

哎呀我錯誤地讀了這個問題:(我沒有標記;) – 2012-04-26 13:38:13