如何在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>
答案 0 :(得分:0)
这是从帖子中获取第一张图片的一种方式..
// 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;
}