Archived topic
Hide sidebar on custom 404 error page
7 replies · Started by Randy on September 30, 2021
Hello,
I've created a custom 404 page using the instructions found here: https://generatepress.com/forums/topic/how-to-redirect-404-error-to-custom-page/
Works well, except how do I hide my right sidebar? Link to site error page below
Thanks!
Hi Randy,
Is the new custom 404 page just a static page?
If so can you try if the sidebar layout options in the metabox work?
https://docs.generatepress.com/article/sidebar-layout/#different-sidebar-layouts-for-individual-pages-and-posts
Let me know :)
No, Leo it's a 404 page that I created using the instructions in the provided link: https://generatepress.com/forums/topic/how-to-redirect-404-error-to-custom-page/
(which I why I originally posted the followup question on that thread :) )
In that case try using a layout element with the same display rule:
https://docs.generatepress.com/article/layout-element-overview/#sidebar
Ok, getting somewhere I think Leo!
I created two Elements:
- A Block-Hook Element set to generate_do_tempalate_part assigned to Location 404 Template: This has the contents of the 404 page
- A Layout Hook Element set to Location 404 Template that has only Content shown (no sidebars)
That successfully hides the sidebar on the 404 error. (Kind of complicated to set up a 404 error page, but if that's the easiest approach, that ok!)
One final follow-up. How do I disable the AdSense ad that is trying to load? The ad is currently loaded as a Hook Element assigned to generate_after_header. It's in a div with the class rlmtopaddiv. How do I disable that on this 404 page?
I tried using something like this in the functions.php, but that didn't work:
add_action('generate_after_header', function() {
if ( is_404() ) {
//not sure what to put here
}
});
Sample page below
The ad is currently loaded as a Hook Element assigned to generate_after_header.
In that case couldn't you just add the 404 page to the exclusion rule?
Ah, great! I didn't realize you could add the 404 Template as an exclusion rule. That worked! Thanks Leo!
No problem :)