How do I move the page title (H1) to header.php (outside of the loop) in a Wordpress theme?

admin2025-06-05  3

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 badges
Add a comment  | 

1 Answer 1

Reset to default 5

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?

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';
    }
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749109878a316443.html

最新回复(0)