zoukankan      html  css  js  c++  java
  • 调用图片

    View Code
     1 //1. 首先在 function.php 中新增函数 catch_that_image
     2 function catch_that_image() {
     3   global $post, $posts;
     4   $first_img = '';
     5   ob_start();
     6   ob_end_clean();
     7   $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
     8   $first_img = $matches [1] [0];
     9   if(empty($first_img)){ //Defines a default image
    10     $first_img = "http://yourwebsite/default.jpg";
    11   }
    12   return $first_img;
    13 }
    14 
    15 //1.1 function.php的另外一种方法
    16 function catch_the_image( $id ) {
    17   // global $post, $posts;
    18   $first_img = '';
    19   // 如果设置了缩略图
    20   $post_thumbnail_id = get_post_thumbnail_id( $id );
    21   if ( $post_thumbnail_id ) {
    22     $output = wp_get_attachment_image_src( $post_thumbnail_id, 'large' );
    23     $first_img = $output[0];
    24   }
    25   else { // 没有缩略图,查找文章中的第一幅图片
    26     ob_start();
    27     ob_end_clean();
    28     $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
    29     $first_img = $matches [1] [0];
    30     if(empty($first_img)){ // 既没有缩略图,文中也没有图,设置一幅默认的图片
    31       $first_img = "http://yourdomain.tld/images/default.jpg";
    32     }
    33   }
    34   return $first_img;
    35 }
    36 //2. 调用 <img src="<?php echo catch_that_image() ?>" alt="" /> //3. 循环调用 <?php if (have_posts()) : ?> <?php query_posts('cat=3' . $mcatID. '&caller_get_posts=1&showposts=6'); ?> <?php while (have_posts()) : the_post(); ?> <li> <p><a href="<?php the_permalink() ?>" ><img src="<?php echo catch_that_image() ?>" alt="" /></a></p> </li> <?php endwhile;?> <?php else : ?> <?php endif; ?>
    37   
  • 相关阅读:
    Git 分支开发规范
    小程序技术调研
    弹性布局
    vue 自定义指令的魅力
    记一次基于 mpvue 的小程序开发及上线实战
    mpvue学习笔记-之微信小程序数据请求封装
    微信小程序开发框架 Wepy 的使用
    Hbuilder 开发微信小程序的代码高亮
    luogu3807 【模板】 卢卡斯定理
    luogu1955 [NOI2015] 程序自动分析
  • 原文地址:https://www.cnblogs.com/iz100/p/3073634.html
Copyright © 2011-2022 走看看