dtqysxw4659 2018-11-08 09:48
浏览 116
已采纳

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

The problem is that on my one pager website the title tag doesn't get populated.

page.php looks like this:

<?php 

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

exit();

page-home.php looks like this:

<?php get_header(); ?>

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

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

<?php //etc. ?>

<?php get_footer();

header.php looks like this:

<!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 -->

And footer.php looks like this:

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

Yes. There is a page present called home in the backend. And I would like Wordpress to pick that title up and use it as a title tag in header.php.

Now as far as I know, Wordpress normally automatically populates the title tag. So what's the problem here? Thanks!

PS: I am not using the wp_title filter anywhere in my custom Wordpress theme.

  • 写回答

1条回答 默认 最新

  • douxiapi4381 2018-11-08 11:18
    关注

    Ok, the solution I found might help others. So I felt free to post it here.

    Part 1. Comparing with the standard twentyseventeen theme. I found that Wordpress needs add_theme_support( 'title-tag' ); to be able to manage the title. (Otherwise you should just add a <title> tag yourself in header.php, I guess.

    Part 2. I needed to add a custom filter to functions.php to display the title in a format that is desired. For example (using a SEO plugin):

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ATAC测序到底用什么peak文件做Diffbind差异分析
  • ¥15 安装ubantu过程中第一个vfat 文件挂载失败
  • ¥20 GZ::CTF如何兼容一些靶机?
  • ¥15 etcd集群部署问题
  • ¥20 谁可以帮我一下问一下各位
  • ¥15 为何重叠加权后love图的SMD与svyCreateTableOne函数绘制基线表的不一致
  • ¥15 QFILHelper怎么恢复全字库,提示进程已完成,只能恢复分区文件
  • ¥150 求 《小魔指》街机游戏机整合模拟软件
  • ¥20 你好,我想问下easyExcel下拉多选,或者复选框可以实现吗
  • ¥20 双非跨考工科哪个专业和方向就业前景好?