experience

经验

zw-1988.com

home > 经验 > wordpress 特色图片

2017.7.12 wordpress 特色图片

zwnet
<img src="<?php
	      $large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'large');
	      if(!empty($large_image_url)){echo $large_image_url[0];}
	      else{
	           echo get_template_directory_uri().'/images/img.png';
		   } ?>" alt="<?php bloginfo('name');
             ?>"/>
($post->ID), 'large'

判断是否有图片 有就输出没有为空

<?php $large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'large');
                     if(!empty($large_image_url)){ ?><img src="<?php echo $large_image_url[0]; ?>" alt="<?php bloginfo('name');
             ?>" />
                    <?php } else {echo "";}   
                    ?>

后面 large 代表的是大尺寸 一般在wordpress里有设置成1024宽
修改成 full 代表的是全尺寸 插入多大图片就是多大
thumbnail 是日志缩略图,代表你主题所设置的缩略图多大就显示多大

这个比较全面 放到function里

function lerm_thumbnail() {
  global $post;
  if (has_post_thumbnail()) {
    // 判断该文章是否已经设置了“特色图像”,如果有则直接显示该特色图像的缩略图
    echo '<a class="thumbnail" href="' . get_permalink() . '" title="' . get_the_title() . '">';
    the_post_thumbnail();
    echo '</a>';
  } else { //如果文章没有设置特色图像,则查找文章内是否包含图片
    $content = $post->post_content;
    preg_match_all('/<img [^>]*src=["|\']([^"|\']+)/i', $content, $strResult, PREG_PATTERN_ORDER);
    $n = count($strResult[1]);
    if ($n > 0) {
      // 如果文章内包含有图片,则取第一张图片的缩略图;
      echo '<a class="thumbnail" href="'. get_permalink() .'" rel="bookmark" title="' . get_the_title() . '"><img src="' . $strResult[1][0] . '" alt="' . get_the_title() . '" class="post-thumb"></a>';
    }else{
      //如果文章内没有图片,则随机显示根目录下images/random文件夹下的一张图片。
      echo '<a class="thumbnail" href="the_permalink()" rel="bookmark" title="' . get_the_title() . '"><img src="'.get_template_directory_uri().'/images/img.jpg" alt="' . get_the_title() . '" class="post-thumb"></a>';
    }
  }
}

直接在需要的地方输出即可

<?php lerm_thumbnail();?>

 

« (NG)
相关文章
wordpress (25)java script (9)tool (8)css (2)攻略 (1)第一次人体 (1)插件 (1)ECharts (1)food (1)雪糕 (1)动画 (1)book (1)标签云 (1)生活 (1)一言难尽的画稿 (1)
辽ICP备16002031号

Copyright © 2016 - 2020 1988-zw.com