属性存在


property exists

此代码:

 $_post = &get_post($post->ID); 
 $classname = ($_post->iconsize[0] <= 128 ? 'small' : '') . 'attachment'; 

有时会产生此错误:

[Sun Apr 15 08:51:35 2012] [error] [client 180.76.5.150] PHP Notice:  Undefined property: stdClass::$iconsize in /srv/www/virtual/myblog.com/htdocs/wp-content/themes/mimbo/attachment.php on line 8

我想修改该行以添加对属性的property_exists检查,如果它不存在,则默认为 '',但对处理属性的语法有点不熟悉。 这条线会是什么样子?

只需使用 isset

if(isset($_post->iconsize)) {
    // ...
}

所以:

<?php
$_post = &get_post($post->ID);
$classname = (isset($_post->iconsize) && $_post->iconsize[0] <= 128 ? 'small' : '') . 'attachment';
?>