我想做一个if语句来测试帖子是否有图片。如果他们这样做,我希望他们做一件事,如果没有,我希望他们做别的事情。你可以在wordpress中测试img标签中的the_content()吗?
有人可以引导我在正确的方向吗?
我想做一个if语句来测试帖子是否有图片。如果他们这样做,我希望他们做一件事,如果没有,我希望他们做别的事情。你可以在wordpress中测试img标签中的the_content()吗?
有人可以引导我在正确的方向吗?
if(($c = get_the_content()) && strstr('<img',$c))
{
//has an image/you can use $c saves calling the function again
}else
{
//No image.
}
这是最快的方法来做到这一点,可能不是100%准确的你。
这实际上导致内容显示两次。 – chris 2010-09-10 21:57:08
是啊,它滑了我的脑海,你必须使用'get_the_content()' – RobertPitt 2010-09-10 22:04:03
我最终使用:
<?php
ob_start();
the_content();
$content = ob_get_clean();
if(!strpos($content, "<img")) {
the_content();
} else {
echo '<img src="' . catch_that_image() . '" alt=""/>';
}
?>
catch_that_image()
是一个自定义功能,我发现只显示来自后的图像。
你可以只验证你为什么需要启动输出缓冲区? – RobertPitt 2010-09-10 21:30:58
否则会显示内容。我只是想把它变成一个变量。也许有一个WordPress的功能,默认情况下,但这个工程。 – chris 2010-09-10 21:47:42
你可以使用'get_the_content' – RobertPitt 2010-09-10 22:03:34
为什么不只是使用内置的特色图像功能?
// in functions.php
add_image_size('my-custom-image-size', width, height);
// in the template
if (has_post_thumbnail()) {
the_post_thumbnail('my-custom-image-size');
} else {
the_content();
}
//or via a filter
function my_image_replacement($the_content) {
global $post;
if (has_post_thumbnail()) {
$the_content = get_the_post_thumbnail($post->ID, 'my-custom-image-size');
// other stuff as necessary
}
return $the_content;
}
add_filter('the_content', 'my_image_replacement', 11);
检查字符串匹配是资源负载吗? – chris 2010-09-10 21:06:08
strstr真的没有任何效果。 – RobertPitt 2010-09-10 21:10:14