WordPress建站教程:如何获取文章中图片的张数

前端之家收集整理的这篇文章主要介绍了WordPress建站教程:如何获取文章中图片的张数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我们在使用wordpress建站的过程中,特别是做图片网站的时候,文章中的图片数量对于增强用户的访问体验是不可或缺的因素,甚至可以左右用户是否进一步访问文章内容。对于套图浏览的网站来说就更为重要了,30多张的套图肯定比3张的套图对于用户更有吸引力。

wordpress网站获取文章中的图片数量是非常实用的一个功能,可以统计文章中的图片有多少张图片。今天我们就介绍一下wordpress网站中获取文章中的图片数量方法

1. 切换到主题目录,打开functions.PHP文件,加入以下获取图片数量函数代码

Crayon-5c891d2d2148e461298921" 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-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-5c891d2d2148e461298921-1">1
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d2d2148e461298921-2">2
Crayon-num" data-line="Crayon-5c891d2d2148e461298921-3">3
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d2d2148e461298921-4">4
Crayon-num" data-line="Crayon-5c891d2d2148e461298921-5">5
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d2d2148e461298921-6">6
Crayon-num" data-line="Crayon-5c891d2d2148e461298921-7">7
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d2d2148e461298921-8">8
Crayon-num" data-line="Crayon-5c891d2d2148e461298921-9">9
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d2d2148e461298921-10">10
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-5c891d2d2148e461298921-1">Crayon-c">// 获取文章图片数量
Crayon-line Crayon-striped-line" id="Crayon-5c891d2d2148e461298921-2">Crayon-t">functionCrayon-h"> Crayon-e">post_img_numberCrayon-sy">(Crayon-sy">)Crayon-sy">{
Crayon-line" id="Crayon-5c891d2d2148e461298921-3">Crayon-h"> Crayon-m">globalCrayon-h"> Crayon-sy">$Crayon-v">postCrayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891d2d2148e461298921-4">Crayon-h"> Crayon-sy">$Crayon-v">post_imgCrayon-h"> Crayon-o">=Crayon-h"> Crayon-s">''Crayon-sy">;
Crayon-line" id="Crayon-5c891d2d2148e461298921-5">Crayon-h"> Crayon-e">ob_startCrayon-sy">(Crayon-sy">)Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891d2d2148e461298921-6">Crayon-h"> Crayon-e">ob_end_cleanCrayon-sy">(Crayon-sy">)Crayon-sy">;
Crayon-line" id="Crayon-5c891d2d2148e461298921-7">Crayon-h"> Crayon-sy">$Crayon-v">outputCrayon-h"> Crayon-o">=Crayon-h"> Crayon-e">preg_match_allCrayon-sy">(Crayon-s">'/\/is'Crayon-sy">,Crayon-sy">$Crayon-v">postCrayon-v">post_contentCrayon-sy">,Crayon-sy">$Crayon-i">matchesCrayon-h"> Crayon-sy">,Crayon-v">PREG_SET_ORDERCrayon-sy">)Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891d2d2148e461298921-8">Crayon-h"> Crayon-sy">$Crayon-v">cntCrayon-h"> Crayon-o">=Crayon-h"> Crayon-e">countCrayon-sy">(Crayon-h"> Crayon-sy">$Crayon-i">matchesCrayon-h"> Crayon-sy">)Crayon-sy">;
Crayon-line" id="Crayon-5c891d2d2148e461298921-9">Crayon-h"> Crayon-st">returnCrayon-h"> Crayon-sy">$Crayon-v">cntCrayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891d2d2148e461298921-10">Crayon-sy">}

2. 在需要显示文章图片数量的地方加入以下代码

Crayon-5c891d2d2149a878479306" 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-5c891d2d2149a878479306-1">1
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-5c891d2d2149a878479306-1">Crayon-ta">PHPCrayon-h"> Crayon-k ">echoCrayon-h"> Crayon-e">post_img_numberCrayon-sy">(Crayon-sy">)Crayon-sy">;

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

猜你在找的wordpress相关文章