Использую в WordPress такой код. Определяет из статьи первую картинку и использует её в качестве картинки-поста. Но в некоторых статьях имеется информационный блок, и в нём картинка "info.png" или "warning.png". Как доработать скрипт, чтобы пропускалась эта картинка ("info.png" или "warning.png"), если она есть.
function get_first_post_image()
{
global $post, $posts;
$first_img = "";
ob_start();
ob_end_clean();
if (
preg_match_all(
'/<img.+src=[\'"]([^\'"]+)[\'"].*>/i',
$post->post_content,
$matches
)
) {
$first_img = $matches[1][0];
return $first_img;
} else {
$first_img = "https://sample.com/images/post-default.png";
return $first_img;
}
}
Вот что у меня получилось, но не работает
function get_post_image()
{
global $post, $posts;
$first_img = "";
ob_start();
ob_end_clean();
if (
preg_match_all(
'/<img.+src=[\'"]([^\'"]+)[\'"].*>/i',
$post->post_content,
$matches
)
) {
if ($matches[1][0] === "info.png" || $matches[1][0] === "warning.png") {
$first_img =
$matches[2][0] ?? "https://sample.com/media/post-default.png";
return $first_img;
} else {
$first_img = $matches[1][0];
return $first_img;
}
}
}
Версия PHP
7.1.11