首页 问答 正文

如何调整wordpress中的404页面?

注册会员 / 悠然自学 / 2023-06-11/ 浏览 132 次

要调整WordPress中的404页面,可以按照以下步骤进行:

  1. 创建自定义404页面
    在WordPress主题文件夹中创建一个新的文件,命名为404.php,并在其中编写自定义404页面的代码。以下是一个示例代码:
<?php get_header(); ?>
<main class="site-main">
  <section class="error-404 not-found">
    <header class="page-header">
      <h1 class="page-title"><?php _e( 'Oops! That page can&rsquo;t be found.', 'mytheme' ); ?></h1>
    </header>
    <div class="page-content">
      <p><?php _e( 'It looks like nothing was found at this location. Maybe try one of the links below or a search?', 'mytheme' ); ?></p>

      <?php get_search_form(); ?>

      <?php the_widget( 'WP_Widget_Recent_Posts' ); ?>

      <div class="widget widget_categories">
        <h2 class="widget-title"><?php _e( 'Most Used Categories', 'mytheme' ); ?></h2>
        <ul>
        <?php
          wp_list_categories( array(
            'orderby'    => 'count',
            'order'      => 'DESC',
            'show_count' => 1,
            'title_li'   => '',
            'number'     => 10,
          ) );
        ?>
        </ul>
      </div>

      <?php
        /* translators: %1$s: smiley */
        $archive_content = '<p>' . sprintf( __( 'Try looking in the monthly archives. %1$s', 'mytheme' ), convert_smilies( ':)' ) ) . '</p>';
        the_widget( 'WP_Widget_Archives', 'dropdown=1', "after_title=</h2>$archive_content" );
      ?>

      <?php the_widget( 'WP_Widget_Tag_Cloud' ); ?>

    </div>
  </section>
</main>
<?php get_footer(); ?> 
  1. 设置WordPress使用自定义404页面
    打开WordPress的functions.php文件,并添加以下代码:
function mytheme_404_page() {
  if ( is_404() ) {
    include( get_template_directory() . '/404.php' );
    exit();
  }
}
add_action( 'template_redirect', 'mytheme_404_page' ); 

以上代码的作用是,如果当前页面是404页面,则自动使用自定义的404页面。

  1. 测试并调整自定义404页面
    保存以上更改,刷新网站,测试自定义404页面是否显示。如果需要进一步调整页面布局或内容,可以修改404.php文件中的代码。

最后,需要注意的是,使用自定义404页面可能对SEO产生影响,需要根据具体情况进行调整,以避免不必要的影响。

大家谈论
    我的见解