繁体   English   中英

wordpress:is_front_page() 用于静态页面不起作用

[英]wordpress: is_front_page() for static page not working

我知道这个问题已经被问过很多次了,但是每次我阅读这些问题的答案时,看起来我已经按照我应该的方式实施了它。 我的问题如下:

我已将以下语句添加到我的 index.php 中,以便为我的静态主页(称为 page-home.php)加载不同的标题(保存在 header-frontpage.php 中)。

if(is_front_page())
{
    get_header('frontpage');
}
else 
{
    get_header();
}

我还在 wordpress 设置->阅读中将 page-home 设置为我的静态首页。

这不应该导致我的主页加载 header-frontpage.php 吗? 现在它也只是为我的首页检索我的 header.php。 可以在 www.koencuijpers.com(header.php 的错误加载)和 www.koencuijpers.com/spot-map(header.php 的正确加载)上找到实时预览。

我的 index.php 的完整代码是:

<?php
/**
 * The main template file.
 *
 * This is the most generic template file in a WordPress theme
 * and one of the two required files for a theme (the other being style.css).
 * It is used to display a page when nothing more specific matches a query.
 * E.g., it puts together the home page when no home.php file exists.
 *
 * @link https://codex.wordpress.org/Template_Hierarchy
 *
 * @package Spot_Utrecht
 */

if(is_front_page())
{
    get_header('frontpage');
}
else 
{
    get_header();
}
 ?>

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

        <?php
        if ( have_posts() ) :

            if ( is_home() && ! is_front_page() ) : ?>
                <header>
                    <h1 class="page-title screen-reader-text"><?php single_post_title(); ?></h1>
                </header>

            <?php
            endif;

            /* Start the Loop */
            while ( have_posts() ) : the_post();

                /*
                 * Include the Post-Format-specific template for the content.
                 * If you want to override this in a child theme, then include a file
                 * called content-___.php (where ___ is the Post Format name) and that will be used instead.
                 */
                get_template_part( 'template-parts/content', get_post_format() );

            endwhile;

            the_posts_navigation();

        else :

            get_template_part( 'template-parts/content', 'none' );

        endif; ?>

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

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

如果您已将主页设置为静态页面,那么 WordPress 将遵循其模板层次结构来决定要使用的模板:

https://developer.wordpress.org/themes/basics/template-hierarchy/#front-page-display

这意味着 WordPress 不会使用 index.php 作为您首页的模板,因此您在那里所做的任何更改都不会产生任何影响。

因此,您需要在适当的模板中包含 if 语句(可能是 front-page.php,取决于您的主题设置)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM