2025-04-22

本文目錄1圖片自動鏈接到文章,添加標題和ALT屬性2關鍵詞自動添加鏈接3第一張圖片加鏈接,其他圖片加alt屬性4實測報告

應 @李輝 朋友的要求,分享一下為WordPress文章的圖片自動添加當前文章鏈接的方法,需要的朋友就自己折騰吧。

圖片自動鏈接到文章,添加標題和ALT屬性

直接將下面的代碼添加到主題的 functions.php 文件即可:

1
2
3
4
5
6
function auto_post_link($content) {
	global $post;
        $content = preg_replace('/<\s*img\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i', "<a href=\"".get_permalink()."\" title=\"".$post->post_title."\" ><img src=\"$2\" alt=\"".$post->post_title."\" /></a>", $content);
	return $content;
}
add_filter ('the_content', 'auto_post_link',0);

function auto_post_link($content) {
global $post;
$content = preg_replace(‘/<\s*img\s+[^>]*?src\s*=\s*(\’|\")(.*?)\\1[^>]*?\/?\s*>/i’, "<a href=\"".get_permalink()."\" title=\"".$post->post_title."\" ><img src=\"$2\" alt=\"".$post->post_title."\" /></a>", $content);
return $content;
}
add_filter (‘the_content’, ‘auto_post_link’,0);

最終的輸出結果如下:

1
2
3
<a href="https://www.wpdaxue.com/wordpress-view-history.html" title="WordPress 添加文章瀏覽歷史功能" >
<img src="/wp-content/images1/20180428/wpdaxue.com-201303521.png" alt="WordPress 添加文章瀏覽歷史功能" />
</a>

<a href="https://www.wpdaxue.com/wordpress-view-history.html" title="WordPress 添加文章瀏覽歷史功能" >
<img src="/wp-content/images1/20180428/wpdaxue.com-201303521.png" alt="WordPress 添加文章瀏覽歷史功能" />
</a>

關鍵詞自動添加鏈接

還可以再添加一個功能,將文章標簽作為關鍵詞,將文章內的關鍵詞自動加上鏈接,有利於SEO,別人復制的時候,就會留下鏈接瞭。在上面的函數裡繼續添加一段代碼即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function auto_post_link($content) {
		global $post;
        $content = preg_replace('/<\s*img\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i', "<a href=\"".get_permalink()."\" title=\"".$post->post_title."\" ><img src=\"$2\" alt=\"".$post->post_title."\" /></a>", $content);
 
	    $posttags = get_the_tags();
	 if ($posttags) {
		 foreach($posttags as $tag) {
			 $link = get_tag_link($tag->term_id); 
			 $keyword = $tag->name;
	   		$content = preg_replace('\'(?!((<.*?)|(<a.*?)))('. $keyword . ')(?!(([^<>]*?)>)|([^>]*?</a>))\'s','<a href="'.$link.'" title="'.$keyword.'">'.$keyword.'</a>',$content,2);//最多替換2個重復的詞,避免過度SEO
		 }
	 }
	   return $content;
}
add_filter ('the_content', 'auto_post_link',0);

function auto_post_link($content) {
global $post;
$content = preg_replace(‘/<\s*img\s+[^>]*?src\s*=\s*(\’|\")(.*?)\\1[^>]*?\/?\s*>/i’, "<a href=\"".get_permalink()."\" title=\"".$post->post_title."\" ><img src=\"$2\" alt=\"".$post->post_title."\" /></a>", $content);
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
$link = get_tag_link($tag->term_id);
$keyword = $tag->name;
$content = preg_replace(‘\'(?!((<.*?)|(<a.*?)))(‘. $keyword . ‘)(?!(([^<>]*?)>)|([^>]*?</a>))\’s’,'<a href="’.$link.’" title="’.$keyword.’">’.$keyword.'</a>’,$content,2);//最多替換2個重復的詞,避免過度SEO
}
}
return $content;
}
add_filter (‘the_content’, ‘auto_post_link’,0);

第一張圖片加鏈接,其他圖片加alt屬性

一樣在functions.php添加:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*--------------------------------------
自動為第一張圖片添加鏈接地址,其他圖片隻加alt屬性,不加鏈接
默認鏈接地址為當前文章的鏈接,alt屬性為當前文章的標題
通過修改判斷語句if($count==1),可以為指定順序圖片添加鏈接,不局限於第一個圖片
--------------------------------------*/
$count = 0;
function auto_image_handler($matches){
	global $count,$post;
	$count++;
	if($count==1){//第一個圖片添加鏈接地址
		return "<a href=\"".get_permalink()."\" title=\"".$post->post_title."\" ><img src=\"$matches[2]\" alt=\"".$post->post_title."\" /></a>";
	}
	else{//其他圖片添加alt屬性
		return "<img src=\"$matches[2]\" alt=\"".$post->post_title."\" /></a>";
	}
}
function auto_post_link($content){
	$content = preg_replace_callback('/<\s*img\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i',
	'auto_image_handler',$content);
	return $content;
}
add_filter ('the_content', 'auto_post_link',0);

/*————————————–
自動為第一張圖片添加鏈接地址,其他圖片隻加alt屬性,不加鏈接
默認鏈接地址為當前文章的鏈接,alt屬性為當前文章的標題
通過修改判斷語句if($count==1),可以為指定順序圖片添加鏈接,不局限於第一個圖片
————————————–*/
$count = 0;
function auto_image_handler($matches){
global $count,$post;
$count++;
if($count==1){//第一個圖片添加鏈接地址
return "<a href=\"".get_permalink()."\" title=\"".$post->post_title."\" ><img src=\"$matches[2]\" alt=\"".$post->post_title."\" /></a>";
}
else{//其他圖片添加alt屬性
return "<img src=\"$matches[2]\" alt=\"".$post->post_title."\" /></a>";
}
}
function auto_post_link($content){
$content = preg_replace_callback(‘/<\s*img\s+[^>]*?src\s*=\s*(\’|\")(.*?)\\1[^>]*?\/?\s*>/i’,
‘auto_image_handler’,$content);
return $content;
}
add_filter (‘the_content’, ‘auto_post_link’,0);

參考資料:http://zhaokaihua.com/405.html

實測報告

1.使用上面的代碼後,由於替代瞭圖片的多餘屬性,將導致圖片的對齊方式失效;

2.由於使用圖片燈箱效果時,需要將圖片鏈接到原始圖片的地址,如果使用該方法將圖片鏈接到文章,那麼燈箱效果就沒辦法使用,請自行取舍。

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *