我正在尝试创建一个动态网站,我有一个带有一些新闻的数据库,我想从帖子内容中获取第一张图片这个新闻是用wordpress添加到这个数据库的,因为你知道wordpress图像在里面post_content.
请你能帮助我获得帖子的第一张图片并发送链接.
谢谢
把它放在你的functions.php文件中:
function get_first_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 = "/images/default.jpg";
}
return $first_img;
}
Run Code Online (Sandbox Code Playgroud)
然后只需调用您需要的页面上的函数来获取帖子的第一个图像,如下所示:
<?php echo get_first_image() ?>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1513 次 |
| 最近记录: |