2010-09-10 60 views

回答

1
if(($c = get_the_content()) && strstr('<img',$c)) 
{ 
    //has an image/you can use $c saves calling the function again 
}else 
{ 
    //No image. 
} 

这是最快的方法来做到这一点,可能不是100%准确的你。

+0

这实际上导致内容显示两次。 – chris 2010-09-10 21:57:08

+1

是啊,它滑了我的脑海,你必须使用'get_the_content()' – RobertPitt 2010-09-10 22:04:03

0

我最终使用:

<?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()是一个自定义功能,我发现只显示来自后的图像。

+0

你可以只验证你为什么需要启动输出缓冲区? – RobertPitt 2010-09-10 21:30:58

+0

否则会显示内容。我只是想把它变成一个变量。也许有一个WordPress的功能,默认情况下,但这个工程。 – chris 2010-09-10 21:47:42

+1

你可以使用'get_the_content' – RobertPitt 2010-09-10 22:03:34

0

为什么不只是使用内置的特色图像功能?

// 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); 
相关问题