Php Wordpress:catch_图像的$content外的标题
我需要在我的wordpress页面的标题上方拉一张图片,所以我使用了catch_那张图片(见下文),但当我这样做时,我失去了在那张图片上的字幕能力。你知道怎么再加进去吗?我认为它与preg_替换或preg_匹配有关,并且/或者因为它不在$content中???以下是我的函数文件中的内容:Php Wordpress:catch_图像的$content外的标题,php,image,wordpress,captions,Php,Image,Wordpress,Captions,我需要在我的wordpress页面的标题上方拉一张图片,所以我使用了catch_那张图片(见下文),但当我这样做时,我失去了在那张图片上的字幕能力。你知道怎么再加进去吗?我认为它与preg_替换或preg_匹配有关,并且/或者因为它不在$content中???以下是我的函数文件中的内容: function catch_that_image() { global $post, $posts; $first_img = ''; ob_start(); ob_end_clean();
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;
}
函数捕捉图像(){
全球$员额,$员额;
$first_img='';
ob_start();
ob_end_clean();
$output=preg_match_all('//i',$post->post_content,$matches);
$first_img=$matches[1][0];
如果(空($first_img)){
$first_img=“”;
}
返回$first\u img;
}
在my 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>
您正在用正则表达式破坏html。。。不要那样做。你能具体说明我的正则表达式在哪里吗?我应该用什么?我没有写代码,只是剪切粘贴了它。谢谢preg\u replace
和preg\u match\u all
。。使用DOM操作操作html/xml。谢谢。这很酷,值得注意,但有人能回答我的问题吗?英雄联盟Marc B,你能告诉我怎么把这个改成DOM ops吗?我不是一个编码员,只知道足够危险…显然。