Всем привет! Пытаюсь разобраться, почему не работает подставление изображения из поста на место обложки. Вот код из functions.php:
//Вывод первой картинки в качестве изображения
function 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(empty($first_img)){
// Указываем изображение поумолчанию
$first_img = "/wp-content/themes/onballoon/img/noimg.png";
}
return $first_img;
}
Вот код, которым пытаюсь вызвать:
<div class="posts row">
<?php
$args = array('post_type' => 'post', 'category' => '-21' , 'orderby' => 'date', 'order' => 'DESC', 'posts_per_page' => 4);
$posts = get_posts($args);
foreach($posts as $post) { ?>
<div class="posts-item col-lg-3 col-md-6 col-sm-6 col-xs-12">
<div class="posts-item-container">
<a href="<?php the_permalink( $post->ID ) ?>">
<div class="posts-item-img">
<?php if ( has_post_thumbnail() ) { the_post_thumbnail('thumbnail'); } else { ?>
<img src="<?php echo first_post_image() ?>" alt="<?php the_title(); ?>">
<?php } ?>
</div>
<div class="posts-item-content">
<h3><?php echo get_the_title( $post->ID ) ?></h3>
<p><?php echo get_the_excerpt( $post->ID ) ?></p>
<p class="posts-item-date"><?php echo get_the_date() ?></p>
</div>
</a>
</div>
</div>
<?php } ?>
</div>
В итоге выводит для всех одно последнее изображение из папки uploads. Как пофиксить?