Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Custom 404 page

  • Hi,

    I’m using GeneratePress on my multilingual WordPress website, and I’m also using WPML to manage translations.

    I have created a custom 404 page for each language using the WordPress editor. I was wondering if it’s possible to modify the default GeneratePress 404 behavior so that it loads the appropriate custom page I created, depending on the current language.

    Here is the default 404 template code from the theme:

    <?php
    /**
     * The template for displaying 404 pages (Not Found).
     *
     * @package GeneratePress
     */
    
    if ( ! defined( 'ABSPATH' ) ) {
    	exit; // Exit if accessed directly.
    }
    
    get_header(); ?>
    
    	<div <?php generate_do_attr( 'content' ); ?>>
    		<main <?php generate_do_attr( 'main' ); ?>>
    			<?php
    			/**
    			 * generate_before_main_content hook.
    			 *
    			 * @since 0.1
    			 */
    			do_action( 'generate_before_main_content' );
    
    			generate_do_template_part( '404' );
    
    			/**
    			 * generate_after_main_content hook.
    			 *
    			 * @since 0.1
    			 */
    			do_action( 'generate_after_main_content' );
    			?>
    		</main>
    	</div>
    
    	<?php
    	/**
    	 * generate_after_primary_content_area hook.
    	 *
    	 * @since 2.0
    	 */
    	do_action( 'generate_after_primary_content_area' );
    
    	generate_construct_sidebars();
    
    	get_footer();

    Is there a way to make this template redirect or display a specific page I created (depending on the current language) instead of the default 404 template part?

    Thanks in advance for your help!

    Thank you,
    Enrico

  • Hi,

    I wanted to follow up on my previous support request regarding setting up a custom 404 page for each language in my multilingual site (using WPML). I have now successfully resolved the issue and no longer require assistance.

    Here’s the solution I implemented:

    I modified the content-404.php file in my GeneratePress child theme to dynamically load the correct 404 page based on the current language. Here’s the code I used:

    <?php
    /**
     * Custom multilingual 404 page for GeneratePress.
     */
    
    if ( ! defined( 'ABSPATH' ) ) {
    	exit; // Exit if accessed directly.
    }
    
    ?>
    <div class="inside-article">
    
    	<?php do_action( 'generate_before_content' ); ?>
    
    	<header <?php generate_do_attr( 'entry-header' ); ?>>
    		<h1 class="entry-title" itemprop="headline"><?php _e( 'Oops! That page can’t be found.', 'generatepress' ); ?></h1>
    	</header>
    
    	<div class="entry-content">
    		<?php
    		// Map custom 404 pages by language
    		$custom_404_pages = [
    			'it' => 123, // Italian 404 page ID
    			'en' => 456, // English 404 page ID
    			'ja' => 789, // Japanese 404 page ID
    		];
    
    		// Get the current language with WPML
    		$current_lang = apply_filters( 'wpml_current_language', NULL );
    
    		// Find the correct 404 page ID
    		$custom_page_id = isset( $custom_404_pages[ $current_lang ] ) ? $custom_404_pages[ $current_lang ] : 123;
    
    		// Display the content of the custom 404 page
    		echo '<div class="custom-404-page">';
    		echo apply_filters( 'the_content', get_post_field( 'post_content', $custom_page_id ) );
    		echo '</div>';
    		?>
    	</div>
    
    	<?php do_action( 'generate_after_content' ); ?>
    
    </div>

    I still want to thank you, and I hope I didn’t take up too much of your time!
    Enrico

  • Hey Enrico, I’m glad to hear you managed to find a solution! 🙂

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.