WordPress wp_title 模版标签

一直以来,广大 WPer 都以 wordpress 强大模板引以为荣,选中自己喜欢的模板后,再进行微调至符合自己的口味。然后就是关注搜索引擎的收录,做相关的所谓 SEO 工作,比如优化标题、安装插件,但是有的模板默认的标题显示格式并不适合搜索引擎的口味,比如:博客标题》日志标题、所有分页页面标题都是博客标题、页面标题为博客标题》页面标题……这些都有个共同的特性就是博客标题显示在最前面,按照有些大湿的说法:重复标题是 SEO 大忌啊!有木有!

今天为大家介绍的就是 wp_title 模版标签,有很多的人都知道,但是未必有好多的人都用上了。自从 wordpress 重新默认 TwentyTen 主题开始到现在的 TwentyEleven 主题,因为均为官方开发的主题,因此很有指导学习意义。里面参考主题 header.php 文件 <title> 的写法,主题无论是首页博客标题,还是文章日志标题、页面标题都进行了优化,具体大家可以切换至默认主题围观。

1、WordPress 3.0版本后默认主题 <title> 的写法,请编辑模板 header.php 文件:

/* 用以下代码替换 header.php 文件 <title> */
<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', '' ), max( $paged, $page ) );

    ?>
</title>

2、需要编辑模板 functions.php 文件的 <title> 的写法(实现效果和1效果一样):

/* 用以下代码替换 header.php 文件 <title> */
<title><?php
    /*
     * Print the <title> tag based on what is being viewed.
     */

    global $page, $paged;

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

    ?>
</title>

将以下代码丢入模板 functions.php 文件:

/* 将以下代码丢入 functions.php 文件 */
function mytheme_filter_wp_title( $title, $separator ) {
    // Don't affect wp_title() calls in feeds.
    if ( is_feed() )
        return $title;

    // The $paged global variable contains the page number of a listing of posts.
    // The $page global variable contains the page number of a single post that is paged.
    // We'll display whichever one applies, if we're not looking at the first page.
    global $paged, $page;

    if ( is_search() ) {
        // If we're a search, let's start over:
        $title = sprintf( __( 'Search results for %s', '' ), '"' . get_search_query() . '"' );
        // Add a page number if we're on page 2 or more:
        if ( $paged >= 2 )
            $title .= " $separator " . sprintf( __( 'Page %s', '' ), $paged );
        // Add the site name to the end:
        $title .= " $separator " . get_bloginfo( 'name', 'display' );
        // We're done. Let's send the new title back to wp_title():
        return $title;
    }

    // Otherwise, let's start by adding the site name to the end:
    $title .= get_bloginfo( 'name', 'display' );

    // If we have a site description and we're on the home/front page, add the description:
    $site_description = get_bloginfo( 'description', 'display' );
    if ( $site_description && ( is_home() || is_front_page() ) )
        $title .= " $separator " . $site_description;

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

    // Return the new title to wp_title():
    return $title;
}
add_filter( 'wp_title', 'mytheme_filter_wp_title', 10, 2 );

因为上述2种方法实现效果一样,所以本人强烈推荐用第1种方法优化标题。当然网上也有某人写了满满一页的代码就为优化博客标题显示,或许也有效果,但是代码也忒多了,我就不推荐了,有兴趣请 Google 然后自己试验。


订阅浩子 有道, 鲜果, 抓虾, 哪吒, 九点, QQ邮箱, Google, 邮件订阅