我需要在我的 wordpress 页面中将图像拉到标题上方,所以我使用了 catch_that_image(见下文)但是当我这样做时,我失去了对该图像的字幕能力。知道如何重新添加吗?我认为它与 preg_replace 或 preg_match 和/或因为它在 $content 之外?这是我的函数文件中的内容:
function catch_that_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 = "";
}
return $first_img;
}
在我的 page.php 文件中:
<div class="entry-content">
<?php
$topimg = catch_that_image(); // Find first image of Post
if($topimg) echo('<img class="top-image" src="'.$topimg.'" alt="OCF Top Image"/>');
?>
<h1 class="entry-title"><?php the_title() ?></h1>
<?php
$content = get_the_content('Read more');
$content = preg_replace('/<img[^>]+./','', $content, 1); // Remove first image of Post
$content = apply_filters('the_content', $content);
echo $content;
?>
</div>