在Wordpress中的自定义函数上使用条件标记/if/else?
如果没有附件,如何隐藏img标签 (函数来自本教程:)在Wordpress中的自定义函数上使用条件标记/if/else?,wordpress,function,wordpress-theming,thumbnails,attachment,Wordpress,Function,Wordpress Theming,Thumbnails,Attachment,如果没有附件,如何隐藏img标签 (函数来自本教程:) “/> 我需要这样的东西: <?php if ( get_attachment_picture()) { ?> <img src="<?php get_attachment_picture();?>"> <?php } else { ?> show nothing, not even av default image <?php } ?> "> 不显示任何内容,甚至不显示av
“/>
我需要这样的东西:
<?php if ( get_attachment_picture()) { ?>
<img src="<?php get_attachment_picture();?>">
<?php } else { ?>
show nothing, not even av default image
<?php } ?>
">
不显示任何内容,甚至不显示av默认图像
您可以让函数返回缩略图,而不是回显缩略图,从而完成所需的操作
从函数中删除以下行:
else:
$related_thumbnail = "images/default_thumbnail.jpg"; //define default thumbnail, you can use full url here.
替换
echo $related_thumbnail;
与
然后,您的代码变为
<?php
$attachment = get_attachment_picture();
if ( ! empty($attachment) ) { ?>
<img src="<?php echo $attachment; ?>">
<?php } ?>
可能值得删除它们,如果您使用的是“标准”文章缩略图,您可以使用has\u post\u缩略图
return $related_thumbnail;
<?php
$attachment = get_attachment_picture();
if ( ! empty($attachment) ) { ?>
<img src="<?php echo $attachment; ?>">
<?php } ?>
ob_start();
ob_end_clean();
<?php
//This must be in one loop
if(has_post_thumbnail()) {
the_post_thumbnail();
} else {
// if there is no thumbnail, do nothing (or whatever you want) here
}
?>