Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

The question title says it all. Myself I just stick with the default code from one of the packaged templates that comes with WordPress out of the box:

<title><?php
    /*
     * Print the <title> tag based on what is being viewed.
     */
    global $page, $paged;

    wp_title( '|', true, 'right' );

    // Add the blog name.
    bloginfo( 'name' );

    // Add the blog description for the home/front page.
    $site_description = get_bloginfo( 'description', 'display' );
    if ( $site_description && ( is_home() || is_front_page() ) )
        echo " | $site_description";

    // Add a page number if necessary:
    if ( $paged >= 2 || $page >= 2 )
        echo ' | ' . sprintf( __( 'Page %s', 'twentyeleven' ), max( $paged, $page ) );

    ?></title>

Do any of you use different code for this?

share|improve this question
    
While it is interesting how people handle this, common (and more practical answer, what if you change theme?) would be using some SEO plugin. –  Rarst Jul 7 '11 at 11:17
add comment

2 Answers 2

Mine is:

  function getDocumentTitle($separator = " &laquo; "){

    $title = get_bloginfo('name');
    $desc = get_bloginfo('description');

    if(is_front_page() && is_home() && !empty($desc)){
      $title .= $separator.$desc;

    }elseif(is_home() || is_singular()){
      $id = $GLOBALS['wp_query']->get_queried_object_id();
      if($meta = get_post_meta($id, 'title', true)) $title .= $separator.$meta;
      $title .= (empty($meta) && is_front_page() && !empty($desc)) ? $separator.$desc : $separator.get_post_field('post_title', $id);

    }elseif(is_archive()){    
      if(is_category() || is_tag() || is_tax()){
        $term = $GLOBALS['wp_query']->get_queried_object();
        $title .= $separator.$term->name;

      }elseif(is_author()){
        $title .= $separator.get_the_author_meta('display_name', get_query_var('author'));

      }elseif(is_date()){
        if(is_day())
          $title .= $separator.sprintf(_a('Archive for %s'), get_the_time(apply_filters('doc_title_archive_day_format', 'F jS, Y')));
        elseif(get_query_var('w'))
          $title .= $separator.$separator.sprintf(_a('Archive for week %1$s of %2$s'), get_the_time('W'), get_the_time('Y'));

        elseif(is_month())
          $title .= $separator.sprintf(_a('Archive for %s'), single_month_title(' ', false));
        elseif(is_year())
          $title .= $separator.sprintf(_a('Archive for year %s'), get_the_time('Y'));

      }else{
        $title .= $separator.post_type_archive_title('', false);

      }

    }elseif(is_search()){
      $title .= $separator.sprintf(_a('Search results for %s'), '&quot;'.get_search_query().'&quot;');

    }elseif(is_404()){
      $title .= $separator._a('404 Not Found');

    }

    // paged?
    if((($page = $GLOBALS['wp_query']->get('paged')) || ($page = $GLOBALS['wp_query']->get('page'))) && $page > 1 && !is_404())
      $title .= $separator.sprintf(_a('Page %s'), $page);

    // comment page?
    if(get_query_var('cpage'))
      $title .= $separator.sprintf(_a('Comment Page %s'), get_query_var('cpage'));

    // apply the wp_title filters so we're compatible with plugins
    return apply_filters('wp_title', $title, $separator, '');
  }
share|improve this answer
add comment
<title><?php wp_title(''); ?><?php if(wp_title('', false)) { echo ' :: '; } ?><?php  bloginfo('name'); if(is_home()) { echo ' :: '; bloginfo('description'); } ?></title>

http://www.nathanrice.net/blog/ultimate-guide-to-wordpress-seo-optimized-titles/

Works for me :)

I try to avoid plugins where possible.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.