Wordpress:使用catch_that_image在$ content之外的标题

时间:2013-10-10 18:34:25

标签: php image wordpress captions

我需要在我的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>

0 个答案:

没有答案