wordpress参考链接

function.php:(加入这段代码)

function catch_that_image() {
    global $post, $posts;
    $first_img = '';
    ob_start();
    ob_end_clean();
    $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
    $first_img = $matches [1] [0];
    if(empty($first_img)){ //Defines a default image
        $first_img = "http://192.168.2.2/wp-content/uploads/2019/10/01e6da22-f471-4c2e-b5f3-b45b43f54025.jpg";
    }
    return $first_img;
}

自定义页面获取图片:

<img src="<?php echo catch_that_image() ?>" alt="" />
发布了91 篇原创文章 · 获赞 14 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/Abenazhan/article/details/102738665