很多朋友发现Joomla的错误页面非常的简单,也不是很利于SEO,所以今天Joomla8特地为大家奉上一篇自定义错误页面的教程,聊表一下Joomla8对大家的心意,也希望大家能一如既往地支持Joomla8。
1.自定义错误页面
Joomla!是使用templates/system/error.php文件来控制HTTP错误的,包括常见的“404无法找到该页错误页面“、”403权限错误页面“和”500错误“等,你可以通过对此文件的修改来实现自定义你的网站的个性错误页面。
首先你需要将templates/system/error.php文件复制到你的templates/<template-name>文件夹下面,如果存在这个文件的话,Joomla系统会默认地以此文件代替系统默认的错误页面。
如果你想要修改错误页面的CSS的话,那么请将templates/system/css/error.css复制到你的templates/<template-name>/css文件夹下面,然后再修改
templates/<template-name>/error.php文件,将文件里面的代码进行一点修改,其实就是将对CSS的解释修改为如下代码:
<link rel="stylesheet" href="<?php echo $this->baseurl; ?>/templates/system/css/error.css" type="text/css" />
然后,如果你会CSS的话,那么就修改那个CSS文件就好了。
2.自定义错误页面信息。
如果你对现在的错误页面不满意的话,当然是可以对其进行修改的,除了上面所说的用替代的方法修改了错误信息外,我们还是可以对其文件里面的代码进行一点点小小的改动的。
例如对404页面的修改:
<?php if ($this->error->code == '404') { ?> <div id="errorboxheader">Page not found</div> <div id="errorboxbody"><p>Sorry! That page cannot be found.</p> </div> </div> <?php } ?>
上面的代码是修改404页面的代码,你可以对其信息修改,当然,只是修改Page not found 和Sorry!That page cannot be found这样的语句了,如果你会PHP的话,完全可以编辑出很不错的404页面。
3.使用模板自带的Header和footer来显示404页面
如果你心细的话,会发现系统自带的404页面是不带有header和footer的,如何在你的404页面上加上你的模板的header和footer呢?首先,在你的
templates/<template-name>/error.php文件里面加入如下代码:
<?php // no direct access defined( '_JEXEC' ) or die( 'Restricted access' ); include dirname(__FILE__) . "/index.php"; ?>
然后,在你的templates/<template-name>/index.php文件里面做如下改动,找到代码:
<jdoc:include type="head" />
将其替换为:
<?php if (!$this->error->code) : ?> <jdoc:include type="head" /> <?php else : ?> <title><?php echo $this->error->code ?> - <?php echo $this->title; ?></title> <?php endif; ?>
再找到如下代码:
<jdoc:include type="component" />
将其替换为:
<?php if ($this->error->code) : /* check if we are on error page, if yes - display error message */ ?> <p><strong><?php echo $this->error->code ?> - <?php echo $this->error->message ?></strong></p> <p><strong><?php echo JText::_('You may not be able to visit this page because of:'); ?></strong></p> <ol> <li><?php echo JText::_('An out-of-date bookmark/favourite'); ?></li> <li><?php echo JText::_('A search engine that has an out-of-date listing for this site'); ?></li> <li><?php echo JText::_('A mis-typed address'); ?></li> <li><?php echo JText::_('You have no access to this page'); ?></li> <li><?php echo JText::_('The requested resource was not found'); ?></li> <li><?php echo JText::_('An error has occurred while processing your request.'); ?></li> </ol> <p><strong><?php echo JText::_('Please try one of the following pages:'); ?></strong></p> <p> <ul> <li><a href="<?php echo $this->baseurl; ?>/index.php" title="<?php echo JText::_('Go to the home page'); ?>"><?php echo JText::_('Home Page'); ?></a></li> </ul> </p> <p><?php echo JText::_('If difficulties persist, please contact the system administrator of this site.'); ?></p> <?php else : ?> <jdoc:include type="component" /> <?php endif; ?>
好了,一切都搞定了,试试看你的错误页面是不是换了一个样子了?
PS:非常重要的一点,做修改前请备份!

