WordPress网站404错误页面教程

WordPress网站404错误页面教程

在搭建WordPress网站的过程中,我们可能会遇到各种各样的问题,我们就来学习一下如何设置一个美观的404错误页面,404错误页面是当用户访问不存在的页面时,服务器返回给用户的提示页面,一个好的404错误页面不仅能够提高用户体验,还能够增强网站的形象,下面,我们就来详细讲解如何设置一个美观的404错误页面。

1、创建一个新的主题

WordPress网站404错误页面教程

我们需要创建一个新的主题,在WordPress后台,依次点击“外观”->“主题”,然后点击“添加新”按钮,在弹出的窗口中,输入新主题的名称,然后点击“添加主题”,接下来,我们就可以在新创建的主题中进行各种设置了。

2、编辑主题文件

在创建新主题之后,我们需要编辑主题文件来设置404错误页面,打开新创建的主题文件夹,找到名为“style.css”的文件,用文本编辑器打开该文件,在其中添加以下代码:

/* 自定义404错误页面样式 */
body {
    background-color: f5f5f5;
}
.container {
    width: 600px;
    margin: 30px auto;
    text-align: center;
}
h1 {
    font-size: 24px;
    color: 333;
}
p {
    font-size: 14px;
    color: 666;
}

这段代码将设置404错误页面的背景颜色、容器样式以及标题和段落的字体大小和颜色,你可以根据自己的喜好进行调整。

WordPress网站404错误页面教程

3、创建自定义页面模板

为了让404错误页面更加个性化,我们还可以创建一个自定义页面模板,在WordPress后台,依次点击“外观”->“模板”,然后点击“添加新”按钮,在弹出的窗口中,输入新模板的名称,然后点击“添加模板”,接下来,我们就可以在新创建的模板中进行各种设置了。

打开新创建的模板文件(位于主题文件夹下的“single.php”),在其中找到以下代码:

<?php get_header(); ?>
<main id="main" class="site-main" role="main">
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
            <?php the_excerpt(); ?>
        </div>
    <?php endwhile; endif; ?>
</main> <!-main -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>

接下来,我们需要在这个代码片段之前插入我们的自定义代码,在<main id="main" class="site-main" role="main">标签之前添加以下代码:

WordPress网站404错误页面教程

<?php get_header(); ?>
<div class="container">
    <?php include get_template_directory() . '/error.php'; ?>
</div> <!-container -->
<?php get_footer(); ?>

<?php include get_template_directory() . '/error.php'; ?>这一行之后添加我们的自定义代码:

<?php include get_template_directory() . '/error.php'; ?>

我们需要创建一个名为“error.php”的新文件,并将其放入主题文件夹下,在这个文件中,我们可以编写我们的自定义404错误页面代码。

<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
    <meta charset="<?php bloginfo( 'charset' ); ?>">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <?php wp_head(); ?>
</head>
<body <?php body_class(); ?>>
<div class="container">
    <?php if ( is_category() || is_tag() || is_tax() || is_post_type_archive() || is_page() || is_date() || is_author() || is_home() || is_search() || is_404() || is_front_page() || is_admin() || is_plugin() || is_singular('') || is_singular('acf') || is_singular('comment') || is_singular('feedback') || is_singular('user') || is_singular('user-profile') || is_singular('media') || is_singular('nav') || is_singular('location') || is_singular('event') || is_singular('course') || is_singular('product') || is_singular('term') || is_singular('utils') || is_singular('widget') || is_singular('yoast_inline') || is_singular('theme') || is_singular('cpt') || is_singular('ctc') || is_singular('taxonomy') || is_singular('post-format') && has_posts() === false && (is_search() || is_404()) && get_query_var('s')): ?>
        <header class="entry-header">
            <h1 class="entry-title"><?php printf( '<span%1$s><span class="screen-reader-text">%2$s</span></span><em>%3$s</em>', ( is_search() || is_404() && get_query_var( 's' ) !== '' && getenv( 'WPSEO_TITLES' ) !== false && getenv( 'WPSEO_TITLES' ) !== '' && apply_filters( 'wpseo_titles', false, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, false) === false && apply_filters( 'wpseo_titles'!=false&&true&&true&&true&&true&&true&&true&&true&&true&&true&&true&&true&&true&&true&&true&&true&&true&&true&&true&&true&&true&&true&&true&&true&&true&&true&&true&&true&&true&&true&&true&&true&&true&&true||false) && apply_filters( 'wpseo_{}_title', false === apply_filters( 'wpseo_{}_title'.get_posttype(), false === apply_filters( 'wpseo_{}_title'.get($post->post_type), false === apply_filters( 'wpseo_{}_title'.get($post->posttype), false === apply__( 'Post Type: '.get($post->posttype), 'inc2734-wpseo' /* translators: %s: post type slug */), false === apply__( 'Post Type: %s'.get($post->posttype), 'inc2734-wpseo' /* translators: %s: post type slug */), false === apply__( 'Post Type: %s'.get($post->posttype), 'inc2734-wpseo' /* translators: %s: post type slug */), false === apply__( 'Post Type: %s'.get($post->posttype), 'inc2734-wpseo' /* translators: %s: post type slug */), false === apply__( 'Post Type: %s'.get($post->posttype), 'inc2734-wpseo' /* translators: %s: post type slug */) /* translators */) !== false && ( $post->posttype !== 'revision' && $post->posttype !== 'nav_menu_item' && $post->posttype !== 'attachment' && $post->posttype !== 'acf' && $post->posttype !== 'comment' && $post->posttype !== 'pingback'), false) && ( $post->posttype !== 'acf' && $post->posttype !== 'comment' && $post->posttype !== 'pingback'), false) && ( $post->posttype !== 'acf' && $post->posttype !== 'comment'

原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/234986.html

Like (0)
Donate 微信扫一扫 微信扫一扫
K-seoK-seo
Previous 2024-01-20 07:04
Next 2024-01-20 07:06

相关推荐

  • 404错误页面的设置方法

    404错误页面的定义404错误页面,又称“Not Found”,是一种HTTP状态码,表示客户端请求的资源在服务器上找不到,当用户访问一个不存在的网页时,服务器会返回一个404错误页面,告知用户所请求的资源未找到,这种错误页面可以帮助用户体验,避免因为找不到所需信息而感到沮丧,404错误页面也有助于搜索引擎优化(SEO),因为搜索引擎……

    2024-01-17
    096
  • 宝塔建站404怎么设置

    您可以在宝塔面板中设置404错误页面。具体步骤如下:进入宝塔后台,选择网站-修改默认页-404错误页,然后根据您的需求进行设置即可 。

    2023-12-28
    0115
  • 服务器请求方式,你了解多少?

    服务器请求方式主要指的是客户端(如浏览器、移动应用等)向服务器发送请求以获取资源或执行操作时所使用的方法,这些请求方式定义了客户端希望对服务器上的资源进行何种操作,例如获取数据、提交数据、更新资源或删除资源等,HTTP/1.1 标准定义了几种常见的请求方法,每种方法都有其特定用途和行为,以下是一些主要的服务器请……

    2024-11-28
    04
  • 如何设置主机宝的404页面? (主机宝404页面设置)

    主机宝是一款专业的网站托管服务,它提供了一系列的功能,包括404页面设置,404页面是当用户访问一个不存在的网页时,服务器返回的错误页面,这个页面的设计和内容对于用户体验至关重要,因为它可以提供有用的信息,帮助用户找到他们想要的内容,以下是如何设置主机宝的404页面的步骤:1、登录主机宝账户:你需要登录你的主机宝账户,在浏览器中输入主……

    2024-03-08
    0100
  • 如何实现静态资源服务器接收post请求? (静态资源服务器的接收post)

    在Web开发中,静态资源服务器通常用于托管CSS、JavaScript、图片等文件,这些文件通常通过GET请求来获取,有时候我们可能需要通过POST请求来发送数据到静态资源服务器,这在某些场景下是有用的,例如在使用AJAX技术进行前后端分离的开发时,我们需要通过POST请求将数据发送到后端服务器,然后由后端服务器将数据发送到静态资源服……

    2024-02-27
    0122
  • GET 和 POST 请求:了解其区别,提升 Web 开发效率和数据安全性

    在Web开发中,GET和POST是两种最常见的HTTP请求方法,它们在数据传输和安全性方面有着显著的区别,理解这些区别可以帮助我们更有效地开发Web应用,并提高数据的安全性。我们来看看GET和POST的基本定义,GET是一种用于请求数据的HTTP方法,它将数据附加到URL的末尾,以查询字符串的形式发送给服务器,而POST则是一种向服务……

    2023-11-15
    0164

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

免备案 高防CDN 无视CC/DDOS攻击 限时秒杀,10元即可体验  (专业解决各类攻击)>>点击进入