Skip to content

[WordPress] Polylang 製作不同語系文章時自動複製來源文章

一直以來,我在 WordPress 當中都是使用著名的 Polylang 外掛來製作中文、英文不同語系的網頁的。通常我都是先寫好中文語系的文章(來源文章),接著為了練習自己的翻譯能力,再打開英文語系的文章,將來源文章的內容複製過去。

但最近由於做了手術,在家休養且十分清閒,故寫文章紀錄學習心得的次數漸漸多了起來,將文章從中文語系完全複製到英文語系的文章再開始翻譯漸漸覺得麻煩了起來。

聽說 Polylang Pro 版中已然支援打開其他語系文章時自動複製來源文章了,不過其實根據 Make Polylang WordPress plugin copy the content from the original post 這篇文章的辦法,一樣可以做到。


解決方法

打開 WordPress 後台,在主題底下找到 functions.php 這份文件並編輯它。我自己的路徑為 public_html/wp_content/themes/neve/functions.php ,不過我的主題自然是 Neve,你需要將想成你自己的主題名稱。

functions.php 這份文件的最底下,加上:

// Polylang
// Make sure Polylang copies the content when creating a translation
function jb_editor_content( $content ) {
    // Polylang sets the 'from_post' parameter
    if ( isset( $_GET['from_post'] ) ) {
        $my_post = get_post( $_GET['from_post'] );
        if ( $my_post )
            return $my_post->post_content;
    }

    return $content;
}
add_filter( 'default_content', 'jb_editor_content' );


// Make sure Polylang copies the title when creating a translation
function jb_editor_title( $title ) {
    // Polylang sets the 'from_post' parameter
    if ( isset( $_GET['from_post'] ) ) {
        $my_post = get_post( $_GET['from_post'] );
        if ( $my_post )
            return $my_post->post_title;
    }

    return $title;
}
add_filter( 'default_title', 'jb_editor_title' );


jb_editor_content() 是複製文章內容、jb_editor_title() 則是複製文章標題。


References


Read More

Tags:

2 thoughts on “[WordPress] Polylang 製作不同語系文章時自動複製來源文章”

Leave a Reply