Wordpress主题会自带404页面,如果需要替换成自定义的需要修改404.php页面,这里以腾讯公益404页面为例介绍下怎样自定义页面,如果大家没有特定的404页面可以设置成公益404页面。

1.找到404页面设置的地方 一般在网站根目录下wp-content/themes/你的主题下,我的主题是sela,所以具体位置是wp-content/themes/sela/404.php

2.备份原页面修改成新的 备份原404.php页面并修改如下:

1
2
3
4
5
6
7
<?php

header("HTTP/1.0 404 Not Found");
include '404.html';
die();

?>

其中404.html内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8" />
<title>公益404</title>
</head>
<body>
<script type="text/javascript" src="//qzonestyle.gtimg.cn/qzone/hybrid/app/404/search_children.js" charset="utf-8" homePageUrl="https://minminmsn.com" homePageName="回到我的主页"></script>

</body>
</html>

3.测试访问效果 随意访问一个不存在的链接如minminmsn.com/404,希望她早日回家!

4.补充搜索结果找不到页时设置404公益页面 修改wp-content/themes/sela/search.php将其中

1
2
3
4
5
6
<?php else : ?>

<?php get_template_part( 'content', 'none' ); ?>

<?php endif; ?>

改为

1
2
3
4
5
<?php else : ?>

<?php header("HTTP/1.0 404 Not Found"); include '404.html'; die(); ?>

<?php endif; ?>

完整内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
/**
* The template for displaying Search Results pages.
*
* @package Sela
*/

get_header(); ?>

<section id="primary" class="content-area">
<main id="main" class="site-main" role="main">

<?php if ( have_posts() ) : ?>

<header class="page-header">
<h1 class="page-title"><?php printf( __( 'Search Results for: %s', 'sela' ), '<span>' . get_search_query() . '</span>' ); ?></h1>
</header><!-- .page-header -->

<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>

<?php get_template_part( 'content', 'search' ); ?>

<?php endwhile; ?>

<?php sela_content_nav( 'nav-below' ); ?>

<?php else : ?>

<?php header("HTTP/1.0 404 Not Found"); include '404.html'; die(); ?>

<?php endif; ?>

</main><!-- #main -->
</section><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>