0

Is it possible to have different 404 templates based on the post_type?

So when page is not found 404.php will be used, when post is not found 404-post.php will be used.

Or alternative, I would like just to display different message when blog post (post_type=post) is not found than when page is not found (post_type=page).

Is there any hook I can use? Codex did not give me any usable hints on this.

0
1

It is possible but wordpress doesn't have any helpful constructs to help with that.

The best approach is to modify your 404.php. Most of the information that you need should be in the main WP_query object and you should be able to do something like

if (is_singular('post_tyoe_a')) {
   ....
} else {
   ....
}

this is extremely untested and you might need to just try to see if enough information is included in the query to do what you want, but if not you can always just check if the URL matches the prefix used for the permalinks of your CPT.

3
  • wp_query will not help me in this case (post_type is empty), is_singular() does not return anything. Yeah, for now I am using the URL checking for /blog/ part (in that case I know it is a post not a page) but not the cleanest solution though. Searching for a better one.. – Janez Aug 21 '14 at 13:42
  • what do you mean in "post_type is empty" ans "is_singular does not return anything"? – Mark Kaplun Aug 21 '14 at 13:45
  • I dumped the global variable $wp_query and echoed is_singular() inside 404.php to check if I can use them – Janez Aug 21 '14 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.