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

(0)
K-seoK-seoSEO优化员
上一篇 2024年1月20日 07:04
下一篇 2024年1月20日 07:06

相关推荐

发表回复

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

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