美文网首页运维点滴
Wordpress设置腾讯公益404页面

Wordpress设置腾讯公益404页面

作者: 三杯水Plus | 来源:发表于2019-12-04 16:16 被阅读0次

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

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

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

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

    其中404.html内容如下:

    <!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将其中
    <?php else : ?>
    
                <?php get_template_part( 'content', 'none' ); ?>
    
            <?php endif; ?>
    
    

    改为

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

    完整内容如下:

    <?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(); ?>
    
    

    相关文章

      网友评论

        本文标题:Wordpress设置腾讯公益404页面

        本文链接:https://www.haomeiwen.com/subject/okelgctx.html