Joomla project supported by william hill poker.

Joomla 2.5 - Create Custom 404 Error Page

Hits: 12514
Print
Last Updated on Saturday, 23 March 2013
  1. On your Joomla 2.5 control panel, add New Article with a 404 title under Uncategorised category.
  2. Add New Menu Item with a 404 Error Page title then link your new article but make the Status Unpublished.
    Click Save & Close.
  3. Edit the newly created menu item 404 Error Page. Copy the Link content (e.g. index.php?option=com_content&view=article&id=121).
  4. Go to your joomla folder and copy error.php from templates/system/ folder to your default template folder (e.g. templates/itropicstemplate/).
  5. Open the newly copied error.php and add this line after the defined('_JEXEC') or die; line.
    if (($this->error->getCode()) == '404') {
    header('Location: index.php?option=com_content&view=article&id=121');
    exit;
    }
    So, your error.php file should look like this.
  6. Replace index.php?option=com_content&view=article&id=121 with your link content as per step # 3.
  7. Visit your website and navigate to a non-existing page to test your custom 404 error page.

Alternative Way

  1. On your default template folder (e.g. templates/itropicstemplate), duplicate your index.php and rename it to error.php
  2. Open your error.php file and add the title line on your head part.
    <head>
    ....
    <title><?php echo $this->error->getCode(); ?> - <?php echo $this->title; ?></title>
    ....
    </head>
  3. Add the jimport line after the line <body>. So it will look similar to this:
    <body>
    <?php jimport( 'joomla.application.module.helper' ); ?>
    ...
    ...
    </body>
  4. On your main content where you normally place the syntax <jdoc:include type="component" />, replace that with your 404 error message like the one below.
    <div style="width:100%;">
    <h2>Oops...</h2>
    <p>Sorry, we could not find the page you were looking for...</p>
    <p>&nbsp;</p>
    <p><a href="http://www.itropics.net">Go back to home page</a></p>
    </div>
  5. Now your menu and other modules will not show up on your 404 Error page. In this case, we need to use the JModuleHelper::getModulesyntax.

    Let say, I want to show the menu module.

    Change:

    <jdoc:include type="modules" name="menu" />

    To:

    <?php $module = JModuleHelper::getModule( 'menu' , 'Main Menu' );
    $attribs['style'] = 'xhtml';
    echo JModuleHelper::renderModule( $module, $attribs); ?>

     



    getModule syntax is getModule( $name, $title ). More info about getModule can be found on http://docs.joomla.org/JModuleHelper/getModule.

    If you want to display a Custom HTML module, $name is custom and $title is your title, so mine would be Footer - Column 1, like:

    <?php
    $module = JModuleHelper::getModule( 'custom' , 'Footer - Column 1' );
    echo JModuleHelper::renderModule( $module);
    ?>


    If you want to display the DisplayNews module, $name is dn.
    If you want to display the Search module, $name is search.

Copyright 2009-2013. iTropics.net