记录生活 学习SEO优化 | 他日若遂凌云志 敢笑黄巢不丈夫
>>Home > 学习笔记 > wordpress判断文章中是否有图片

wordpress判断文章中是否有图片

Tags: | | 作者:海天无影

wordpress判断文章中是否有图片,这篇文章其实应该是配合《图片适当载入渐显特效,基于jQuery.Lazyload》这篇文章一起发出来的,原谅我今天才想起来……为什么说要配合那篇文章发布呢?OK,看我慢慢道来。

因为上一篇文章是为了快速加载有图片的页面,但是没有图片的页面?我们又不是图片站,既然没有图片还要加载个不大不小的JS文件,感觉就有点累赘了吧。本文就是判断文章页面中是否有图片,如果有,才加载图片渐显的JS。

wordpress判断文章中是否有图片,用以下代码就可以完成:

<?php
$content = $post->post_content;
$searchimages = '~<img [^>]* />~';
/*用preg_match_all函数来检查日志中是否有<img>标签,并把检查结果存储在$pics中*/
preg_match_all( $searchimages, $content, $pics );
// 看看是否有至少1张图片
$iNumberOfPics = count($pics[0]);
if ( $iNumberOfPics > 0 ) {

echo '<script type="text/javascript" src="http://blog.haitianhome.com/wp-content/themes/prowerV3.1/jquery.lazyload.js"></script>
<script type="text/javascript">
jQuery(document).ready(
function($){
$(".contents img").lazyload({
placeholder : "http://blog.haitianhome.com/wp-content/themes/prowerV3.1/image/fill.gif",
effect : "fadeIn"
});
});
</script>
'; }
?>

其中红色部分是判断语句,浅绿色部分是判断有图片才需要加载的代码,也就是上一篇文章《图片适当载入渐显特效,基于jQuery.Lazyload》所用的代码。

有的朋友可能会说JS我都是在家页面最后的没事。的确,海天的博客的图片点击显示效果,也是将JS放在页面最后的,这个是没有影响的。

但是如果你用《图片适当载入渐显特效,基于jQuery.Lazyload》这个的话,那就不行了,因为你将图片都全部加载完成了,还需要那个JS放在最后干嘛呢?所以还是必须得放在页面开始来加载的,加个判断,免去了不必要的加载。

日志信息 »

该日志于2011-01-09 14:27由 海天无影 发表在学习笔记分类下, 你可以发表评论。除了可以将这个日志以保留源地址及作者的情况下转载到你的网站或博客,还可以通过RSS 2.0订阅这个日志的所有评论。

36条评论▼点击进行评论


  1. Warning: copy(/home/wwwroot/haitian/blog/avatar/81a0e7218a90e9235e12f8ea60db4ace.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    不知道是为了实现什么功能


    • Warning: copy(/home/wwwroot/haitian/blog/avatar/3d58c5baea42043cdd0f012c22cfd774.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
      回复

      是为了让没有图片的文章 少加载一些JS 呵呵
      主题呢 我 要你的主题 哈哈


  2. Warning: copy(/home/wwwroot/haitian/blog/avatar/fb2142a1ebd9dd2d0dc5d012fd84ded5.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    还有其他的方法呢。我用的主题中又一个函数,能够获取文章中图片的第一张。不过代码没你的短小, :lol:


    • Warning: copy(/home/wwwroot/haitian/blog/avatar/3d58c5baea42043cdd0f012c22cfd774.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
      回复

      恩 获取到第一张图片肯定也判断出来啦 呵呵


  3. Warning: copy(/home/wwwroot/haitian/blog/avatar/244714b7504e1a40f3d2c71ffb4c8877.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    :oops: 貌似见过这样的插件 le ~


    • Warning: copy(/home/wwwroot/haitian/blog/avatar/3d58c5baea42043cdd0f012c22cfd774.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
      回复

      额 插件吗?这个本身就是判断没有图片就不调用的
      你再来加个插件 貌似有点累赘啊


  4. Warning: copy(/home/wwwroot/haitian/blog/avatar/d94967b298c3fcfed3f8e5a6c5181f42.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    这个功能很强大啊,不过我用不上 :lol:


    • Warning: copy(/home/wwwroot/haitian/blog/avatar/3d58c5baea42043cdd0f012c22cfd774.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
      回复

      呵呵 就是一个判断 像我的博客有时候会有图片 加上去好点


  5. Warning: copy(/home/wwwroot/haitian/blog/avatar/c338dbe71bccbcc6c35befd199894fbf.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    海天今天更新文章 小托也今天 哈哈哈 :lol:


    • Warning: copy(/home/wwwroot/haitian/blog/avatar/3d58c5baea42043cdd0f012c22cfd774.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
      回复

      呵呵 我最近好懒的 ;-)


  6. Warning: copy(/home/wwwroot/haitian/blog/avatar/bf12d703481f992fb051334a04f27c7b.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    这个想法很好~


  7. Warning: copy(/home/wwwroot/haitian/blog/avatar/5b22c1d1c3d4391c7ad2681c1f03e750.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    可以借鉴一下 以后会用的到


  8. Warning: copy(/home/wwwroot/haitian/blog/avatar/fad14a832ca37404b8e610f4179d0be0.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    我也会判断,用我的眼睛行不? :mrgreen:


    • Warning: copy(/home/wwwroot/haitian/blog/avatar/3d58c5baea42043cdd0f012c22cfd774.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
      回复

      额 你判断完……再告诉程序那就迟了


  9. Warning: copy(/home/wwwroot/haitian/blog/avatar/e79b68291d7cca7f06d4a71c11c1607a.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    好办法,有时间是应该优化一下模板了


    • Warning: copy(/home/wwwroot/haitian/blog/avatar/3d58c5baea42043cdd0f012c22cfd774.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
      回复

      呵呵 必须的


  10. Warning: copy(/home/wwwroot/haitian/blog/avatar/ae1d9cb02574d579039efc62a447f2f3.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    我每篇文章都配图的耶~


    • Warning: copy(/home/wwwroot/haitian/blog/avatar/3d58c5baea42043cdd0f012c22cfd774.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
      回复

      额 那到是不需要了


  11. Warning: copy(/home/wwwroot/haitian/blog/avatar/aeac975dd070cf20c8fdb3167502e144.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    呵呵 以前做过 现在不需要了! :mrgreen:


    • Warning: copy(/home/wwwroot/haitian/blog/avatar/3d58c5baea42043cdd0f012c22cfd774.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
      回复

      呵呵


  12. Warning: copy(/home/wwwroot/haitian/blog/avatar/80c6778d63af710963c2c43c81bbe833.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    网站图片不错 先不用了


  13. Warning: copy(/home/wwwroot/haitian/blog/avatar/39e15fb57dceeaf6b13d33b10bd74c2f.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    学习了


  14. Warning: copy(/home/wwwroot/haitian/blog/avatar/dcaa3b50ec35e14119ffca8a35693273.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    考虑要不要加个图片渐显效果。。。


    • Warning: copy(/home/wwwroot/haitian/blog/avatar/3d58c5baea42043cdd0f012c22cfd774.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
      回复

      呵呵 这个可以有


  15. Warning: copy(/home/wwwroot/haitian/blog/avatar/d5d4e154f90574748e3d7a7001c98fcf.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    JS就是累赘啊!


  16. Warning: copy(/home/wwwroot/haitian/blog/avatar/49ca679cb38873e4d6fa159c4768a53e.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    好久不见啦


    • Warning: copy(/home/wwwroot/haitian/blog/avatar/3d58c5baea42043cdd0f012c22cfd774.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
      回复

      哈哈是啊 松松 你都不来我这个博客


  17. Warning: copy(/home/wwwroot/haitian/blog/avatar/19b65993f18cafb9c55362b4c34f631d.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    原来如此


  18. Warning: copy(/home/wwwroot/haitian/blog/avatar/94319958a1466624675952173097a8b0.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    给个友情连接把 会继续关注你的~


  19. Warning: copy(/home/wwwroot/haitian/blog/avatar/8a696fb2b60f79867e39e7803303fc7a.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    又是蛮实用的一个功能,等以后往CMS转型的时候可以用上。


    • Warning: copy(/home/wwwroot/haitian/blog/avatar/3d58c5baea42043cdd0f012c22cfd774.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
      回复

      额 你也准备用WP做CMS?


  20. Warning: copy(/home/wwwroot/haitian/blog/avatar/79628fafd7df23711a9937eb1a06226e.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    :shock: 我的文章也基本有图


    • Warning: copy(/home/wwwroot/haitian/blog/avatar/3d58c5baea42043cdd0f012c22cfd774.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
      回复

      呵呵 那就不需要了


  21. Warning: copy(/home/wwwroot/haitian/blog/avatar/12b9ca311887d0c56ef18c36e8dd5fbd.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    这样确实是可以提高加载速度


    • Warning: copy(/home/wwwroot/haitian/blog/avatar/3d58c5baea42043cdd0f012c22cfd774.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
      回复

      恩 肯定会有那么点的 呵呵


  22. Warning: copy(/home/wwwroot/haitian/blog/avatar/2d9d17892d31e395319fccb3b046d42a.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    俺的图片不多


    • Warning: copy(/home/wwwroot/haitian/blog/avatar/3d58c5baea42043cdd0f012c22cfd774.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
      回复

      呵呵 你的纯技术的 那也不需要


  23. Warning: copy(/home/wwwroot/haitian/blog/avatar/62ec9fd39264a68421dacfb2003ce015.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    博主最近没有更新啊!


    • Warning: copy(/home/wwwroot/haitian/blog/avatar/3d58c5baea42043cdd0f012c22cfd774.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
      回复

      额 最近比较懒


  24. Warning: copy(/home/wwwroot/haitian/blog/avatar/2667e3794c00b147ea9cda10b88365ae.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    再加强下。判断是不是站内的链接。


    • Warning: copy(/home/wwwroot/haitian/blog/avatar/3d58c5baea42043cdd0f012c22cfd774.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
      回复

      这个貌似还在那个判断中加 应该可以实现
      不过要看图片是不是本地的干妈啊


  25. Warning: copy(/home/wwwroot/haitian/blog/avatar/ec52c029d7e97ea0daaf113a05fd1bb6.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    哈~海天现在也是PHP程序员了! :mrgreen:


    • Warning: copy(/home/wwwroot/haitian/blog/avatar/3d58c5baea42043cdd0f012c22cfd774.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
      回复

      额 哪里 :oops:


  26. Warning: copy(/home/wwwroot/haitian/blog/avatar/62ec9fd39264a68421dacfb2003ce015.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    以前这个问题我也在考虑啊 现在我算是明白点了啊 有没有再详细点的啊 在好好的指教我一下 谢谢了啊


    • Warning: copy(/home/wwwroot/haitian/blog/avatar/3d58c5baea42043cdd0f012c22cfd774.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
      回复

      额 再详细的……不知道怎么详细了


  27. Warning: copy(/home/wwwroot/haitian/blog/avatar/152f90fdd68816e41c53dab837931b2a.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    更新慢,我一般很少用图片!


    • Warning: copy(/home/wwwroot/haitian/blog/avatar/3d58c5baea42043cdd0f012c22cfd774.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
      回复

      呵呵 最近很懒了


  28. Warning: copy(/home/wwwroot/haitian/blog/avatar/5e29ff2eb17b2447c034c302eff3a6e1.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    好像用处不是很大


    • Warning: copy(/home/wwwroot/haitian/blog/avatar/3d58c5baea42043cdd0f012c22cfd774.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
      回复

      就是让不需要加载js不加载


  29. Warning: copy(/home/wwwroot/haitian/blog/avatar/40778ff2c73f96323537a02861912213.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    嗯,不错,来关注支持下!


  30. Warning: copy(/home/wwwroot/haitian/blog/avatar/5787d439183a1a95bd9f182fbbff44f6.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    恩,挺好,以后可能能用到


  31. Warning: copy(/home/wwwroot/haitian/blog/avatar/79a9e817c5e3dfd97ce5bda425a1450f.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    感觉php很厉害啊。。。。


  32. Warning: copy(/home/wwwroot/haitian/blog/avatar/06bc2d703214f45eae49453cd3252322.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    图片渐显的效果很不错,在很多其他博客上也见到了。


  33. Warning: copy(/home/wwwroot/haitian/blog/avatar/97f7a2b0d362160accc65c5c2011494e.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    不错的想法


  34. Warning: copy(/home/wwwroot/haitian/blog/avatar/4a9d7802f10e37e777f10bead49af063.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    嗯,不错,来关注支持下!


  35. Warning: copy(/home/wwwroot/haitian/blog/avatar/13d55566118cdd6475228418bb84d87e.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    感谢分享


  36. Warning: copy(/home/wwwroot/haitian/blog/avatar/68c4a63498a1702dfa2b270ef28208c2.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    谢谢楼主!

icon_wink.gif icon_neutral.gif icon_mad.gif icon_twisted.gif icon_smile.gif icon_eek.gif icon_sad.gif icon_rolleyes.gif icon_razz.gif icon_redface.gif icon_surprised.gif icon_mrgreen.gif icon_lol.gif icon_idea.gif icon_biggrin.gif icon_evil.gif icon_cry.gif icon_cool.gif icon_arrow.gif icon_confused.gif icon_question.gif icon_exclaim.gif 

« 元旦、南京、非诚勿扰、莫愁湖、秦淮河 phpcms多图模型调用 »