我如何过滤我的帖子,特色或在WordPress中的第一个图像,并在显示前添加到主题的css类。我看到我可以在我的functions.php中使用API中的add_filter()函数,但是我在获取每个帖子的第一张图片时遇到了问题。
<a href="http://localhost/wordpress/wp-content/uploads/2012/10/test.jpg">
<img class="alignnone size-full wp-image-95" width="540" height="300" alt="dell-vs-apple" src="http://localhost/wordpress/wp-content/uploads/2012/10/dell-vs-apple1.jpg">
</a>
<div class = "my_class">
<a href="http://localhost/wordpress/wp-content/uploads/2012/10/dell-vs-apple1.jpg">
<img class="alignnone size-full wp-image-95" width="540" height="300" alt="dell-vs-apple" src="http://localhost/wordpress/wp-content/uploads/2012/10/dell-vs-apple1.jpg">
</a>
发布于 2013-02-14 21:11:14
这是从post中获取第一张图片的一种方法。
// Get URL of first image in a post
function o99_find_first_post_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 no image found we can choose to display some default image instead
if(empty($first_img)){
$first_img = "/images/default.jpg";// path to default image
}
return $first_img;
}https://stackoverflow.com/questions/14870528
复制相似问题