admin管理员组文章数量:1130349
I am trying to put my page title in header.php because I want it to print before the initial content div. To my surprise this has proved quite challenging.
- I have tried using the_title(), and this works in most places, but on category pages for example it pulls the name of the first post instead.
- I have tried using wp_title(), but my site titles have my site name in them, and I don't want that to be in my H1s.
- I know I can do a bunch of if statements and just make sure I cover every scenario, but that seems messy, surely there's a cleaner and more future proof option?
Thanks very much in advance.
I am trying to put my page title in header.php because I want it to print before the initial content div. To my surprise this has proved quite challenging.
- I have tried using the_title(), and this works in most places, but on category pages for example it pulls the name of the first post instead.
- I have tried using wp_title(), but my site titles have my site name in them, and I don't want that to be in my H1s.
- I know I can do a bunch of if statements and just make sure I cover every scenario, but that seems messy, surely there's a cleaner and more future proof option?
Thanks very much in advance.
Share Improve this question asked Dec 11, 2018 at 3:18 TelFiRETelFiRE 1135 bronze badges1 Answer
Reset to default 5I know I can do a bunch of if statements and just make sure I cover every scenario, but that seems messy, surely there's a cleaner and more future proof option?
Nope! It is a bit messy. Unfortunately there's no single function for outputting a title for all page types.
There's essentially 4 'types' of pages in WordPress that will need different titles:
- Single posts (or pages)
- Archives
- Search results
- 404
So you could write your own function that you could put in header.php that would output an appropriate title for each type of page:
function wpse_321605_title() {
if ( is_singular() ) {
$queried_object_id = get_queried_object_id();
echo get_the_title( $queried_object_id );
} else if ( is_archive() ) {
the_archive_title();
} else if ( is_search() ) {
echo 'Searching for: ' . esc_html( get_search_query() );
} else if ( is_404() ) {
echo 'Page Not Found';
}
}
However, a lot of people don't like that the_archive_title() prefixes category and tag archives with "Category:" and "Tag:", so if you don't want those prefixes you'll need to handle taxonomy archives separately and use single_term_title():
function wpse_321605_title() {
$queried_object_id = get_queried_object_id();
if ( is_singular() ) {
echo get_the_title( $queried_object_id );
} else if ( is_tax() || is_tag() || is_category() ) {
single_term_title()
} else if ( is_archive() ) {
the_archive_title();
} else if ( is_search() ) {
echo 'Searching for: ' . esc_html( get_search_query() );
} else if ( is_404() ) {
echo 'Page Not Found';
}
}
I am trying to put my page title in header.php because I want it to print before the initial content div. To my surprise this has proved quite challenging.
- I have tried using the_title(), and this works in most places, but on category pages for example it pulls the name of the first post instead.
- I have tried using wp_title(), but my site titles have my site name in them, and I don't want that to be in my H1s.
- I know I can do a bunch of if statements and just make sure I cover every scenario, but that seems messy, surely there's a cleaner and more future proof option?
Thanks very much in advance.
I am trying to put my page title in header.php because I want it to print before the initial content div. To my surprise this has proved quite challenging.
- I have tried using the_title(), and this works in most places, but on category pages for example it pulls the name of the first post instead.
- I have tried using wp_title(), but my site titles have my site name in them, and I don't want that to be in my H1s.
- I know I can do a bunch of if statements and just make sure I cover every scenario, but that seems messy, surely there's a cleaner and more future proof option?
Thanks very much in advance.
Share Improve this question asked Dec 11, 2018 at 3:18 TelFiRETelFiRE 1135 bronze badges1 Answer
Reset to default 5I know I can do a bunch of if statements and just make sure I cover every scenario, but that seems messy, surely there's a cleaner and more future proof option?
Nope! It is a bit messy. Unfortunately there's no single function for outputting a title for all page types.
There's essentially 4 'types' of pages in WordPress that will need different titles:
- Single posts (or pages)
- Archives
- Search results
- 404
So you could write your own function that you could put in header.php that would output an appropriate title for each type of page:
function wpse_321605_title() {
if ( is_singular() ) {
$queried_object_id = get_queried_object_id();
echo get_the_title( $queried_object_id );
} else if ( is_archive() ) {
the_archive_title();
} else if ( is_search() ) {
echo 'Searching for: ' . esc_html( get_search_query() );
} else if ( is_404() ) {
echo 'Page Not Found';
}
}
However, a lot of people don't like that the_archive_title() prefixes category and tag archives with "Category:" and "Tag:", so if you don't want those prefixes you'll need to handle taxonomy archives separately and use single_term_title():
function wpse_321605_title() {
$queried_object_id = get_queried_object_id();
if ( is_singular() ) {
echo get_the_title( $queried_object_id );
} else if ( is_tax() || is_tag() || is_category() ) {
single_term_title()
} else if ( is_archive() ) {
the_archive_title();
} else if ( is_search() ) {
echo 'Searching for: ' . esc_html( get_search_query() );
} else if ( is_404() ) {
echo 'Page Not Found';
}
}
本文标签: How do I move the page title (H1) to headerphp (outside of the loop) in a Wordpress theme
版权声明:本文标题:How do I move the page title (H1) to header.php (outside of the loop) in a Wordpress theme? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749109854a2317198.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论