将返回值表示为"if"条件



我想在代码中写,如果$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' )返回false,它在div中显示图像。

可能是这样的:

<?php if (condition returns false) { 
echo '<div><img src="image source"></div>'
} ?>

我该怎么做呢?

我想你是在找! ("not")运算符。

if (condition)  // Branches when `condition` is true
if (!condition) // Branches when `condition` is false

!反转布尔值。即!TRUEFALSE, !FALSETRUE

如果你正在处理一个你使用==而不是!的条件,你将使用!=。例如,如果你想把这个颠倒过来:

if ($a == $b)

你会这样做:

if (!$a == $b) // <== WRONG

你会这样做:

if ($a != $b)

这也是有效的,但往往比必要的更复杂:

if (!($a == $b)) // (note the parentheses so that ! applies to the result of ==)

try this

$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
if($image===false)
{
    echo '<div><img src="image source"></div>';
}

你可以简单地在PHP的if语句中使用你的函数:

<?php 
if (!wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' )) 
{ 
    echo "<div><img src='image source'></div>"
} ?>

!翻转if条件,使if TRUE do...变为if FALSE do...

你可以像这样嵌入html到php .

<?php if(!$condition) : ?>
 echo '<div><img src="image source"></div>'
<?php endif; ?>

最新更新