• 调用图片


    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   
  • 相关阅读:
    Remove Element leetcode java
    Remove Duplicates from Sorted Array leetcode java
    Container With Most Water leetcode java
    Divide Two Integers leetcode java
    N-Queens II leetcode java
    N-Queens leetcode java
    Pow(x,n) leetcode java
    Single Number II leetcode java
    Single Number leetcode java
    ROS第一次开网站跳转到公告页(任意地址跳转)方法
  • 原文地址:https://www.cnblogs.com/iz100/p/3073634.html
Copyright © 2020-2023  润新知