Strange WordPress Attachment Page Error

I was running into a strange WordPress error on attachment pages that were linked from a gallery embed on a normal blog post, and only for unauthenticated users.  This happened in the both the custom theme as well as the default theme of the site in question under WordPress 2.9.2

The error was “Warning: Attempt to assign property of non-object in /path/to/wp-content/link-template.php on line 999″

The offending code looked like this:

if ( empty($post->post_title)  )
$post->post_title = $previous ? __('Previous Post') : __('Next Post');

Even though the theme the site is using didn’t have a custom Image Attachment Template (image.php), the default theme does, so it led me to believe the issue lied with the core files.  Essentially, it seems as though the post object wasn’t being set, and since I haven’t yet had the time to delve into a more proper method for resolving the error, I simply changed the if conditions to the following.

if ( empty($post->post_title) && !is_attachment() )
$post->post_title = $previous ? __('Previous Post') : __('Next Post');

That cleared the error so at least I can get on with my life, and look into the real source of the problem when some free time shakes loose, preferrably before the next WordPress Upgrade boils the error back to the surface.

This entry was posted in Development and tagged , . Bookmark the permalink.

One Response to Strange WordPress Attachment Page Error

  1. Robin says:

    We have never met but now I love you. Thanks for this.

Leave a Reply

Your email address will not be published. Required fields are marked *

Connect with Facebook

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>