| 主機參考:VPS測評參考推薦/專注分享VPS服務器優惠信息!若您是商家可以在本站進行投稿,查看詳情!此外我們還提供軟文收錄、PayPal代付、廣告贊助等服務,查看詳情! |
| 我們發布的部分優惠活動文章可能存在時效性,購買時建議在本站搜索商家名稱可查看相關文章充分了解該商家!若非中文頁面可使用Edge瀏覽器同步翻譯!PayPal代付/收錄合作 |
WordPress站長在發布文章時往往不注意給圖片添加描述(ALT),導致文章中大量圖片缺少ALT屬性,不利于SEO。網上有很多自動給文章和圖片添加ALT屬性的教程。這里有一個國外的方法供參考。
WordPress只需要將下面的代碼添加到當前的主題功能模板functions.php中。
函數回調($buffer) { /*在這里修改buffer,然后返回更新后的代碼*/$ title = ' ';$ RES = preg _ match('/& lt;title & gt(.*?)& lt/title & gt;/',$buffer,$ title _ matches);if($ RES){/*清理標題:刪除EOL和過多的空白。*/ $title = preg_replace('/s+/',' ',$ title _ matches & # 0911]);$ title = trim($ title);} preg _ match _ all('/& lt;img(。*?)/& gt;/',$buffer,$ images);如果(!is _ null($ images)){ foreach($ images & # 091;1] as $index = >$value) { preg_match('/alt= "(。*?)"/',$value,$ img);preg_match('/alt= '(。*?)'/',$value,$ img 2);如果(!is_null($images)) { if((!isset($ img & # 091;1])| | $ img & # 091;1] == '') || (!isset($ img 2 & # 091;1])| | $ img 2 & # 091;1]= = ' '){ $ new _ img = str _ replace(' & lt;img ',' & ltimg alt= " "。$title。“‘,$ images & # 0910][$ index]);$ buffer = str _ replace($ images & # 091;0][$index],$new_img,$ buffer);} } } }返回$ buffer}函數buffer _ start(){ ob _ start();}函數buffer_end() { echo回調(ob _ get _ clean());} add_action('wp ',' buffer_start ',0);add_action('wp_footer ',' buffer _ end ');雖然在代碼中添加了緩沖區,但還是會降低效率。建議安裝靜態緩存插件。
其他方法:
函數img _ alt($ content){ global $ post;preg _ match _ all('/& lt;img(。*?)/& gt;/',$content,$ images);如果(!is _ null($ images)){ foreach($ images & # 091;1] as $index = >$ value){ $ new _ img = str _ replace(' & lt;img ',' & ltimg alt= " "。get_the_title()。'-'.get_bloginfo('name '),'“標題=”。get_the_title()。'-'.get_bloginfo('name '),'“‘,$ images & # 0910][$ index]);$ content = str _ replace($ images & # 091;0][$index],$new_img,$ content);} } return $ content} add_filter('the_content ',' img_alt ',99999);
這幾篇文章你可能也喜歡:
- 將WordPress之前的文章轉換成古騰堡區塊
- WordPress媒體庫創建分類,文件夾創建(如何修改WordPress上傳媒體限制)
- 從WordPress文章中批量導入外部鏈接圖片到本地。
- 打開SSL(https)后,WordPress站點提示“重定向你太多次”的解決方案
- 在wordpress主題中添加自定義文章類型register_post_type和分類教程(WordPress文章類型)。
本文由主機參考刊發,轉載請注明:使用標題標簽作為WordPress文章圖片的替換(WordPress原始標簽插件) http://www.k2270.com/86359.html
主機參考
















評論前必須登錄!
注冊