当WordPress文章中没有设置特色图片时,读取文章中的第一张图片

前端之家收集整理的这篇文章主要介绍了当WordPress文章中没有设置特色图片时,读取文章中的第一张图片前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

wordpress建站过程中,很多时候,我们的文章中没有设置特色图片,但是我们想显示出来这篇文章中的第一张图片,下面介绍一下方法

1. 切换到你主题所在目录,打开function.PHP文件,加入下面这段代码

注:此方法工作原理是查找文章内有没img这个标签,如果有就调出第一张图片,如果没有就用一张设计好的图片代替

Crayon-5c891d54ecb1c040905009" class="Crayon-Syntax Crayon-theme-classic Crayon-font-monaco Crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouSEOver" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
Crayon-toolbar" data-settings=" mouSEOver overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">Crayon-title">functions.PHP
Crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
Crayon-button Crayon-nums-button" title="Toggle Line Numbers">
Crayon-button-icon">
Crayon-button Crayon-plain-button" title="Toggle Plain Code">
Crayon-button-icon">
Crayon-button Crayon-wrap-button" title="Toggle Line Wrap">
Crayon-button-icon">
Crayon-button Crayon-expand-button" title="Expand Code">
Crayon-button-icon">
Crayon-button Crayon-copy-button" title="Copy">
Crayon-button-icon">
Crayon-button Crayon-popup-button" title="Open Code In New Window">
Crayon-button-icon">
Crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
Crayon-plain-wrap">
Crayon-main" style="">Crayon-table">Crayon-row">
Crayon-nums " data-settings="show">
Crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
Crayon-num" data-line="Crayon-5c891d54ecb1c040905009-1">1
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d54ecb1c040905009-2">2
Crayon-num" data-line="Crayon-5c891d54ecb1c040905009-3">3
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d54ecb1c040905009-4">4
Crayon-num" data-line="Crayon-5c891d54ecb1c040905009-5">5
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d54ecb1c040905009-6">6
Crayon-num" data-line="Crayon-5c891d54ecb1c040905009-7">7
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d54ecb1c040905009-8">8
Crayon-num" data-line="Crayon-5c891d54ecb1c040905009-9">9
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d54ecb1c040905009-10">10
Crayon-num" data-line="Crayon-5c891d54ecb1c040905009-11">11
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d54ecb1c040905009-12">12
Crayon-num" data-line="Crayon-5c891d54ecb1c040905009-13">13
Crayon-code">
Crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
Crayon-line" id="Crayon-5c891d54ecb1c040905009-1">Crayon-t">functionCrayon-h"> Crayon-e">catch_that_imageCrayon-sy">(Crayon-sy">)Crayon-h"> Crayon-sy">{
Crayon-line Crayon-striped-line" id="Crayon-5c891d54ecb1c040905009-2">Crayon-h">Crayon-m">globalCrayon-h"> Crayon-sy">$Crayon-v">postCrayon-sy">,Crayon-h"> Crayon-sy">$Crayon-v">postsCrayon-sy">;
Crayon-line" id="Crayon-5c891d54ecb1c040905009-3">Crayon-h">Crayon-sy">$Crayon-v">first_imgCrayon-h"> Crayon-o">=Crayon-h"> Crayon-s">''Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891d54ecb1c040905009-4">Crayon-h">Crayon-e">ob_startCrayon-sy">(Crayon-sy">)Crayon-sy">;
Crayon-line" id="Crayon-5c891d54ecb1c040905009-5">Crayon-h">Crayon-e">ob_end_cleanCrayon-sy">(Crayon-sy">)Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891d54ecb1c040905009-6">Crayon-h">Crayon-sy">$Crayon-v">outputCrayon-h"> Crayon-o">=Crayon-h"> Crayon-e">preg_match_allCrayon-sy">(Crayon-s">'//i'Crayon-sy">,Crayon-h"> Crayon-sy">$Crayon-v">postCrayon-v">post_contentCrayon-sy">,Crayon-h"> Crayon-sy">$Crayon-v">matchesCrayon-sy">)Crayon-sy">;
Crayon-line" id="Crayon-5c891d54ecb1c040905009-7">Crayon-h">Crayon-sy">$Crayon-v">first_imgCrayon-h"> Crayon-o">=Crayon-h"> Crayon-sy">$Crayon-i">matchesCrayon-h"> Crayon-sy">[Crayon-cn">1Crayon-sy">]Crayon-h"> Crayon-sy">[Crayon-cn">0Crayon-sy">]Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891d54ecb1c040905009-8">
Crayon-line" id="Crayon-5c891d54ecb1c040905009-9">Crayon-h">Crayon-st">ifCrayon-sy">(Crayon-e">emptyCrayon-sy">(Crayon-sy">$Crayon-v">first_imgCrayon-sy">)Crayon-sy">)Crayon-sy">{Crayon-h"> Crayon-c">//Defines a default image
Crayon-line Crayon-striped-line" id="Crayon-5c891d54ecb1c040905009-10">Crayon-h">Crayon-sy">$Crayon-v">first_imgCrayon-h"> Crayon-o">=Crayon-h"> Crayon-e">get_bloginfoCrayon-sy">(Crayon-s">'template_url'Crayon-sy">)Crayon-sy">.Crayon-s">"/images/default.jpg"Crayon-sy">;
Crayon-line" id="Crayon-5c891d54ecb1c040905009-11">Crayon-h">Crayon-sy">}
Crayon-line Crayon-striped-line" id="Crayon-5c891d54ecb1c040905009-12">Crayon-h">Crayon-st">returnCrayon-h"> Crayon-sy">$Crayon-v">first_imgCrayon-sy">;
Crayon-line" id="Crayon-5c891d54ecb1c040905009-13">Crayon-sy">}

2. 在想调用的地方,加入下面这段代码

Crayon-5c891d54ecb26804361902" class="Crayon-Syntax Crayon-theme-classic Crayon-font-monaco Crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouSEOver" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
Crayon-toolbar" data-settings=" mouSEOver overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">Crayon-title">
Crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">Crayon-mixed-highlight" title="Contains Mixed Languages">
Crayon-button Crayon-nums-button" title="Toggle Line Numbers">
Crayon-button-icon">
Crayon-button Crayon-plain-button" title="Toggle Plain Code">
Crayon-button-icon">
Crayon-button Crayon-wrap-button" title="Toggle Line Wrap">
Crayon-button-icon">
Crayon-button Crayon-expand-button" title="Expand Code">
Crayon-button-icon">
Crayon-button Crayon-copy-button" title="Copy">
Crayon-button-icon">
Crayon-button Crayon-popup-button" title="Open Code In New Window">
Crayon-button-icon">
Crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
Crayon-plain-wrap">
Crayon-main" style="">Crayon-table">Crayon-row">
Crayon-nums " data-settings="show">
Crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
Crayon-num" data-line="Crayon-5c891d54ecb26804361902-1">1
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d54ecb26804361902-2">2
Crayon-num" data-line="Crayon-5c891d54ecb26804361902-3">3
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d54ecb26804361902-4">4
Crayon-code">
Crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
Crayon-line" id="Crayon-5c891d54ecb26804361902-1">Crayon-ta">PHPCrayon-h">
Crayon-line Crayon-striped-line" id="Crayon-5c891d54ecb26804361902-2">Crayon-h"> Crayon-v">$aCrayon-o">=Crayon-e">catch_that_imageCrayon-sy">(Crayon-sy">)Crayon-sy">;
Crayon-line" id="Crayon-5c891d54ecb26804361902-3">Crayon-h"> Crayon-k ">echoCrayon-h"> Crayon-s">'Crayon-sy">.Crayon-v">$aCrayon-sy">.Crayon-s">'" alt="" style="width:150px;"/>'Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891d54ecb26804361902-4">

原文链接:https://www.f2er.com/wordpress/238520.html

猜你在找的wordpress相关文章