0

这段代码:

 $_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 检查属性并默认为 '' 如果它不存在但对处理属性的语法有点不熟悉。这条线看起来如何?

4

1 回答 1

1

只需使用isset

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

所以:

<?php
$_post = &get_post($post->ID);
$classname = (isset($_post->iconsize) && $_post->iconsize[0] <= 128 ? 'small' : '') . 'attachment';
?>
于 2012-04-15T03:48:47.657 回答