您使用什么代码来生成要进入HTML标题标记的文本?

时间:2011-07-07 作者:Scott

问题标题说明了一切。我自己坚持使用WordPress现成的打包模板中的默认代码:

<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>
你们有没有人对此使用不同的代码?

2 个回复
SO网友:onetrickpony

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, \'\');
  }
SO网友:DOA

<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/

为我工作:)

我尽量避免使用插件。

结束

相关推荐

SANITIZE_TITLE_WITH_DASSES格式化函数是否过于自由(就接受的字符而言)?

sanitize_title_with_dashes (参考见下面的代码)是Wordpress用来格式化“漂亮”URL的函数。然而,与函数的注释头相反,它允许的不仅仅是字母数字字符、下划线(\\u0)和破折号(-)。它还允许诸如°、等标志。我如何才能真正做到只允许字母数字字符和破折号?/** * Sanitizes title, replacing whitespace with dashes. * * Limits the output to alphanumeric ch