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   
  • 相关阅读:
    windows对象的属性和方法
    JavaScript事件处理
    HTML骨架-深入理解
    js阻止元素的默认事件与冒泡事件
    CSS3之背景色渐变
    CSS3匹配屏幕横竖状态
    LINUX系统GIT使用教程
    博客园文章页显示布局调整
    JS编码解码详解
    JS设置获取cookies
  • 原文地址:https://www.cnblogs.com/iz100/p/3073634.html
Copyright © 2011-2022 走看看