Products
GG网络技术分享 2025-03-18 16:12 2
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.
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;
}
Demand feedback