Wordpress - 从get_the_content获取标题和图片 [英] Wordpress - get only title and image from get_the_content

查看:1091
本文介绍了Wordpress - 从get_the_content获取标题和图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在帮朋友寻找一个页面显示所有帖子,只显示标题和帖子中的第一个嵌入图片。不是特色图片(或后缩略图),而是一个作为帖子内容的一部分。

I'm doing a favor for a friend looking to have a page with all the posts displayed with only the title and the first embedded image from the post. Not a featured image (or post-thumbnail), but one that is part of the post content.

因为它有利于我没有请求FTP访问,我只是使用display posts插件,它使用get_the_content来显示帖子。

Since it's a favor I haven't requested FTP access and am simply using the "display posts" plugin which uses get_the_content to display the posts.

有没有办法(正则表达式?)来提取图像?我可以明显地从其他方法中获得标题,但是如果图像是嵌入式的呢?是否仍然附加并可以从get_post_thumbnail等使用?

Is there a way (regex?) to extract the image? I can get the title from other methods obviously, but what if the image is embedded? Is it still attached and available to use from get_post_thumbnail, etc?

感谢您的帮助!

推荐答案

在这里:

<?php while ( have_posts() ) : the_post(); ?>
    <?php preg_match("#<img(.+?)src=(.+?)\/>#", $post->post_content, $matches);
    /** $matches is an array, $matches[0] holds the img code */
echo $matches[0]; ?>
<?php the_content();?>
<?php endwhile; // end of the loop. ?>

这篇关于Wordpress - 从get_the_content获取标题和图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆