$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>How do I move the page title (H1) to header.php (outside of the loop) in a Wordpress theme?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin7PV0评论

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';
    }
}
Post a comment

comment list (0)

  1. No comments so far