Append your blog description to the window title in Wordpress
I wanted my homepage title to reveal my writing goals and interests. The importance of the title, to me, is obvious:
- It’s one of the first things people notice.
- It’s visible in the window chrome, the tabs, the bookmarks
- It’s something search engines pay attention to
Wordpress settings allow you to set the blog title and description. With this modification, the description will be appended whenever a post title is not displayed, to avoid making it extremely long.
Edit the functions.php file of your template and append:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | add_filter('wp_title', 'dv_wp_title', 100, 3); $dv_append_subtitle = false; function dv_wp_title($title) { global $dv_append_subtitle; if($title === '') $dv_append_subtitle = true; return $title; } function dv_the_subtitle($sep = '—') { global $dv_append_subtitle; if($dv_append_subtitle) { echo $sep ? $sep . ' ' : ''; bloginfo('description'); } } |
Then go to header.php and make your title tag look like this (notice that I added the dv_the_subtitle call)
<title><?php wp_title('«', true, 'right'); ?> <?php bloginfo('name'); ?> <?php dv_the_subtitle(); ?></title>
And that’s it! If you want to customize the separator, which defaults to — pass it as an argument to dv_the_subtitle.