[WordPress 3] 以Child Theme機制修改Twenty Eleven佈景主題為中文版

為了把偏重家庭生活的內容分離到另一個網站,因此以WordPress 3.3架設了另一個網站,這個網站直接採用WordPress預設的Twenty Eleven佈景主題。Twenty Eleven版本已經是1.3版,似乎WordPress主系統有較大變動時,Twenty Eleven也會改版,網站架設好後有許多文字仍然是英文,因此研究了如何修改佈景主題而又不受版本更新而影響到方法-最妥善又一勞為逸的應該是使用Child Theme的機制。

###Child Theme
Child Theme就是以繼承已有主題來做局部客製的概念,WordPress自3.0開始就提供了Child Theme的功能,建立子主題的步驟很簡單:

  1. 在wp-content/themes/建立新的資料夾,例如建立 twentyeleven-child
  2. 在新建資料夾建立一定要有的style.css,檔案開頭必須有下列內容;Template: 指定由那個主題來繼承。style.css或任何存放入資料夾裡的檔案記得要儲存成UTF-8格式,以免中文變成亂碼。完整的Child Theme說明可以參考英文說明簡體中文說明
/*
Theme Name:     Twenty Eleven Child
Template:       twentyeleven
*/
@import url("../twentyeleven/style.css"); /*非必要,但建議要加*/

3.由後台的【外觀】→【佈景主題】啟用新建的Child Theme。
4.

啟用子主題後檢視前台,原先設定好的選單不見了,只剩首頁、關於本網誌。只要到後台的【外觀】→【選單】,將主要選單選為【主選單】再儲存即可。

5.在新建資料夾裡放入functions.php,將Posted on 日期 by 作者的英文字串修改成中文:

<?php
function twentyeleven_posted_on() {
  printf( __( '<span class="sep">發表日期:</span><a href="%1$s" title="%2$s" rel="bookmark"><time class="entry-date" datetime="%3$s" pubdate>%4$s</time></a><span class="by-author"> <span class="sep"> 作者:</span> <span class="author vcard"><a class="url fn n" href="%5$s" title="%6$s" rel="author">%7$s</a></span></span>', 'twentyeleven' ),
    esc_url( get_permalink() ),
    esc_attr( get_the_time() ),
    esc_attr( get_the_date( 'c' ) ),
    esc_html( get_the_date() ),
    esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
    esc_attr( sprintf( __( '檢視 %s 的所有文章', 'twentyeleven' ), get_the_author() ) ),
    get_the_author()
  );
}
?>

6.在新建資料夾裡放入content.php(由Twentyeleven資料夾複製,應該有更好的作法才對)將Posted in 分類 | Leave a reply修改成中文:

 
<?php
/**
 * The default template for displaying content
 *
 * @package WordPress
 * @subpackage Twenty_Eleven
 * @since Twenty Eleven 1.0
 */
?>

  <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    <header class="entry-header">
      <?php if ( is_sticky() ) : ?>
        <hgroup>
          <h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyeleven' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
          <h3 class="entry-format"><?php _e( 'Featured', 'twentyeleven' ); ?></h3>
        </hgroup>
      <?php else : ?>
      <h1 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyeleven' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h1>
      <?php endif; ?>

      <?php if ( 'post' == get_post_type() ) : ?>
      <div class="entry-meta">
        <?php twentyeleven_posted_on(); ?>
      </div><!-- .entry-meta -->
      <?php endif; ?>

      <?php if ( comments_open() && ! post_password_required() ) : ?>
      <div class="comments-link">
        <?php comments_popup_link( '<span class="leave-reply">' . __( 'Reply', 'twentyeleven' ) . '</span>', _x( '1', 'comments number', 'twentyeleven' ), _x( '%', 'comments number', 'twentyeleven' ) ); ?>
      </div>
      <?php endif; ?>
    </header><!-- .entry-header -->

    <?php if ( is_search() ) : // Only display Excerpts for Search ?>
    <div class="entry-summary">
      <?php the_excerpt(); ?>
    </div><!-- .entry-summary -->
    <?php else : ?>
    <div class="entry-content">
      <?php the_content( __( '閱讀全文... <span class="meta-nav">»</span>', 'twentyeleven' ) ); ?>
      <?php wp_link_pages( array( 'before' => '<div class="page-link"><span>' . __( 'Pages:', 'twentyeleven' ) . '</span>', 'after' => '</div>' ) ); ?>
    </div><!-- .entry-content -->
    <?php endif; ?>

    <footer class="entry-meta">
      <?php $show_sep = false; ?>
      <?php if ( 'post' == get_post_type() ) : // Hide category and tag text for pages on Search ?>
      <?php
        /* translators: used between list items, there is a space after the comma */
        $categories_list = get_the_category_list( __( ', ', 'twentyeleven' ) );
        if ( $categories_list ):
      ?>
      <span class="cat-links">
        <?php printf( __( '<span class="%1$s">分類:</span> %2$s', 'twentyeleven' ), 'entry-utility-prep entry-utility-prep-cat-links', $categories_list );
        $show_sep = true; ?>
      </span>
      <?php endif; // End if categories ?>
      <?php
        /* translators: used between list items, there is a space after the comma */
        $tags_list = get_the_tag_list( '', __( ', ', 'twentyeleven' ) );
        if ( $tags_list ):
        if ( $show_sep ) : ?>
      <span class="sep"> | </span>
        <?php endif; // End if $show_sep ?>
      <span class="tag-links">
        <?php printf( __( '<span class="%1$s">標籤:</span> %2$s', 'twentyeleven' ), 'entry-utility-prep entry-utility-prep-tag-links', $tags_list );
        $show_sep = true; ?>
      </span>
      <?php endif; // End if $tags_list ?>
      <?php endif; // End if 'post' == get_post_type() ?>

      <?php if ( comments_open() ) : ?>
      <?php if ( $show_sep ) : ?>
      <span class="sep"> | </span>
      <?php endif; // End if $show_sep ?>
      <span class="comments-link"><?php comments_popup_link( '<span class="leave-reply">' . __( '留言', 'twentyeleven' ) . '</span>', __( '<b>1</b> Reply', 'twentyeleven' ), __( '<b>%</b> Replies', 'twentyeleven' ) ); ?></span>
      <?php endif; // End if comments_open() ?>

      <?php edit_post_link( __( 'Edit', 'twentyeleven' ), '<span class="edit-link">', '</span>' ); ?>
    </footer><!-- #entry-meta -->
  </article><!-- #post-<?php the_ID(); ?> -->

就這幾個步驟就把Twenty Eleven主題變成中文版了。

###範例網站
* 簡睿的美好新人生

##

您可能也會有興趣的類似文章

簡睿

服務於軟體業的資訊老兵。興趣廣泛,學習力佳,樂於分享所知所學。

您可能也會喜歡…