Different titles for WordPress blog page and static front page

I was a little surprised when I realized that wp_title() doesn't return the page title for a static WordPress front page or the blog page. I don't like that, because I want at least the blog page to have a label. Something like... blog :-D or maybe news.

So I started to search a little and found no good solution. This post has a solution, but it's not optimal. I like to have a template that can be used across several blogs, so everything has to be dynamic. To make my version more dynamic Otto's comment on this thread was useful. So here it is:

<?php
if ( is_front_page() ) {
    echo get_the_title(get_option('page_on_front')) . " - ";
} elseif ( is_home() ) {
    echo get_the_title(get_option('page_for_posts')) . " - ";
}
bloginfo('name');

This is example is not a complete solution as it doesn't handle is_archive(), is_search() and friends and there's no HTML markup. But it should be easy to adapt the idea to your own template.Update: This is what I'm using now, it works on blogs that have a static frontpage and on blogs that don't have one.

<?php
if ( is_archive() ) {
    echo 'Archives - ';
} elseif ( is_search() ) {
    echo 'Search - ';
} elseif ( is_404() ) {
    echo '404 - ';
} elseif ( is_front_page() && !is_home() ) {
    echo get_the_title(get_option('page_on_front')) . " - ";
} elseif ( is_home() && !is_front_page() ) {
    echo get_the_title(get_option('page_for_posts')) . " - ";
} else {
    wp_title(' - ', true, 'right');
}
bloginfo('name');
?>

5 comments

  1. avatar
    wrote this comment on

    Most SEO plugins have a graphical interface for this

    1. avatar
      wrote this comment on

      That may be true, but it's much better to build correctly SEOed themes to begin with. This post is intended for theme developers.

  2. avatar
    wrote this comment on
    Hi,
    found the post above very informative and am setting about chaning our code. I have one question though and I hope that you can help me. I have the code above working and the static front page title works perfectly now and remains ststic, which is what I want and also it puts in the name of the post as the title on the single page which is good too but it appends onto the title on the single page, the name of the blog. If you understand what I mean, is there any way of just having the name of the post as the title on the single page without appending on the blog name to it. Ideally we wnat to keep the title below 70 characters and having the blog name added to it amkes it way longer. Hope you can help. Thanks.

    Dave
    1. avatar
      wrote this comment on
      Sure this can be done. I guess there are several SEO plugins if you don't know PHP. The conditional for posts is is_single() and PHP's substr() can give you a shorter version of get_the_title().
  3. avatar
    wrote this comment on
    THANK YOU! You just saved me from pulling my hair out!

Start a new thread

Cancel reply
Markdown. Syntax highlighting with <code lang="php"><?php echo "Hello, world!"; ?></code> etc.