I have a custom post type called projects, and its archive is at /projects, on the archive page I get notices Notice: Trying to get property of non-object every time I try to access the $post i.e. $post->post_name. Is this intended behaviour? Should I just accept that and always check the availability of $post before using it?

if (!empty($post)) {
    // Do something with $post
}

2 Answers
2

Put in the following line at the top of archive page :

global $post;

Thing is you need to access the global $post object before trying to use it.

Tags:

Leave a Reply

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