Wordpress wp_head() 标题标签不起作用

Wordpress wp_head() title tag not working

问题是在我的单页导航网站上没有填充标题标签。

page.php 看起来像这样:

<?php 

locate_template( 'page-home.php', true );

exit();

page-home.php 看起来像这样:

<?php get_header(); ?>

<?php get_template_part('template-sections/slider'); ?>

<?php get_template_part('template-sections/services'); ?>

<?php //etc. ?>

<?php get_footer();

header.php 看起来像这样:

<!doctype html>
<html <?php language_attributes(); ?>>
<head>
    <meta charset="<?php bloginfo( 'charset' ); ?>"/>
    <meta name="viewport" content="width=device-width, initial-scale=1"/>
    <link rel="profile" href="http://gmpg.org/xfn/11"/>
    <base href="<?php echo get_site_url(); ?>/">
    <?php wp_head(); ?>
</head>

<body itemprop="hasPart" itemscope="" itemtype="http://schema.org/WebPage" <?php body_class(); ?>>
<!-- etc -->

footer.php 看起来像这样:

            <?php wp_footer(); ?>
    </main>
    <aside class="aside">
        <div class="aside__content">
        </div>
    </aside>
</div>
</body>
</html>

是的。后端有一个名为 home 的页面。我希望 Wordpress 选择该标题并将其用作 header.php.

中的标题标签

据我所知,Wordpress 通常会自动填充标题标签。那么这里的问题是什么?谢谢!

PS:我没有在我的自定义 Wordpress 主题中的任何地方使用 wp_title 过滤器。

好的,我找到的解决方案可能会对其他人有所帮助。所以我觉得可以在这里 post 它。

第 1 部分。与标准 twentyseventeen 主题比较。我发现 Wordpress 需要 add_theme_support( 'title-tag' ); 才能管理标题。 (否则你应该自己在 header.php 中添加一个 <title> 标签,我想。

第 2 部分。我需要向 functions.php 添加一个 自定义过滤器 以便以所需的格式显示标题。例如(使用 SEO 插件):

function custom_title( $title_parts ) {
    $page_id   = site_get_page_id(); // custom function, you might want to use global $post here
    $seo_title = @get_post_meta( $page_id, '_aioseop_title' );

    if ( isset( $seo_title[0] ) ) {
        $title = $seo_title[0];
    } elseif ( isset( $page_id ) ) {
        $title = get_the_title( $page_id );
    }

    $page_title           = isset( $title ) ? $title : 'Page not found in backend';
    $title_parts['title'] = $page_title;

    return $title_parts;
}