/"> > footer.php 看起来像这样-6ren">
gpt4 book ai didi

php - Wordpress wp_head() 标题标签不工作

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:42:44 26 4
gpt4 key购买 nike

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

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 过滤器。

最佳答案

好的,我找到的解决方案可能会对其他人有所帮助。所以我可以随意将它张贴在这里。

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

第 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;
}

关于php - Wordpress wp_head() 标题标签不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53205142/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com