wordpress給文章圖片自動添加鏈接
來源:易賢網(wǎng) 閱讀:1143 次 日期:2014-10-21 10:07:52
溫馨提示:易賢網(wǎng)小編為您整理了“wordpress給文章圖片自動添加鏈接”,方便廣大網(wǎng)友查閱!

我們會看到有很多地網(wǎng)站我們點擊圖片就會進入當(dāng)前文件連接了,下面我來給使用wordpress博客地同學(xué)也來介紹此種辦法.

圖片自動鏈接到文章,添加標(biāo)題和alt屬性

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

代碼如下:

function auto_post_link($content) {

global $post;

$content = preg_replace('/<s*imgs+[^>]*?srcs*=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);

最終地輸出結(jié)果如下:

代碼如下:

<a href=wordpress-view-history.html title=wordpress 添加文章瀏覽歷史功能 >

<img src=201303521.png alt=wordpress 添加文章瀏覽歷史功能 />

</a>

關(guān)鍵詞自動添加鏈接

還可以再添加一個功能,將文章標(biāo)簽作為關(guān)鍵詞,將文章內(nèi)地關(guān)鍵詞自動加上鏈接,有利于seo,別人復(fù)制地時候,就會留下鏈接了.在上面地函數(shù)里繼續(xù)添加一段代碼即可

代碼如下:

function auto_post_link($content) {

global $post;

$content = preg_replace('/<s*imgs+[^>]*?srcs*=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個重復(fù)地詞,避免過度seo

}

}

return $content;

}

add_filter ('the_content', 'auto_post_link',0);

更多信息請查看IT技術(shù)專欄

更多信息請查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機網(wǎng)站地址:wordpress給文章圖片自動添加鏈接
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點

版權(quán)所有:易賢網(wǎng)