Try `if ($node->status == 0)` instead of `if ($unpublished)`. This may be happening because the variable is not set correctly in a `hook_preprocess_node()` implementation of the theme; you may also want to check the template.php file in your theme for these hooks.