wordpress之缩略图调用

2013.11.04 分享 14090 人浏览 4 条留言

说明:

相比与传统的wp缩略图(大多数主题作者使用的方法),这个集中了所有内容,首先是判断是否带特色图片,有则输出,无就判断是否文章内有图片,有就调用首图,无则调用默认图片(可调用随机文章,见15 16行注释部分);还有一个好处就是,这个是直接输出缩略图的地址的,这样可以在外部定义缩略图的尺寸,及定义各种id class还有你懂的...

  1. <?php
  2. function huilang_thumbnail(){
  3.     global $post;
  4.     if ( has_post_thumbnail() ){
  5.         $domsxe = simplexml_load_string(get_the_post_thumbnail($post->ID,'medium',''));
  6.         $thumbnailsrc = $domsxe->attributes()->src;
  7.         echo $thumbnailsrc;
  8.     } else {
  9.         $content = $post->post_content;
  10.         preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content$strResult, PREG_PATTERN_ORDER);
  11.         $n = count($strResult[1]);
  12.         if($n > 0){
  13.             echo $strResult[1][0];
  14.         }else {
  15.             /*$random = mt_rand(1, 10);
  16.             echo get_bloginfo('template_url').'/img/thumb/img'.$random.'.png';*/
  17.             echo get_bloginfo('template_url').'/img/default.jpg';
  18.         }
  19.     }
  20. }
  21. ?>

调用:

  1. <?php huilang_thumbnail( ); ?>

例:

  1. <img width="100" height="100" src="<?php huilang_thumbnail( ); ?>" alt="<?php the_title(); ?>" />

这样俺制作的主题又可以轻便一点了...

文章地址:https://huilang.me/the-thumbnails-wordpress-call/

“wordpress之缩略图调用” 有 4 条评论

  1. 诸葛小觉说道:

    你终于恢复博客更新了= =

回复本文

电子邮件地址不会被公开。 必填项已用*标注

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