0

This code:

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

sometimes produces this error:

[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

I would like to modify the line to add the property_exists check on the property and default to '' if it doesn't exist but am a little unfamiliar with the syntax dealing with properties. How would the line look?

nathanjosiah
  • 4,441
  • 4
  • 35
  • 47
macmiller
  • 325
  • 1
  • 4
  • 15
  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – animuson Apr 15 '12 at 03:49

1 Answers1

1

Just use isset:

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

So:

<?php
$_post = &get_post($post->ID);
$classname = (isset($_post->iconsize) && $_post->iconsize[0] <= 128 ? 'small' : '') . 'attachment';
?>
Ry-
  • 218,210
  • 55
  • 464
  • 476