如何復制 WordPress 帖子或頁面?
已發表: 2020-11-03複製頁面或帖子使構建網站變得毫不費力。 它不僅僅是複制和粘貼內容。
WordPress 帖子和頁面複製可以幫助您通過單擊調整主題設置、SEO 數據和圖像。
有多個實例需要構建頁面或帖子,但內容除外,它們完全相似。 例如,只需更改一些內容即可創建具有相同外觀和感覺的銷售頁面。
想像一下設置一次設置並再次將其用於其他頁面會有多有趣?
那會很棒,對吧?
在下面的文章中,您將找到在 WordPress 中復制帖子或頁面的方法。 您將了解如何手動操作、使用插件和不使用插件。
目錄
- 為什麼要復制 WordPress 帖子或頁面?
- 如何復制 WordPress 帖子或頁面?
- 使用插件
- Yoast重複帖子
- 重複頁面
- 重複頁面和帖子
- 不使用插件
- 通過functions.php代碼
- 手動複製和粘貼代碼
- 結論
為什麼要復制 WordPress 帖子或頁面?
在構建網站時,有時,您網站中的兩個或更多頁面的設置可能會變得相同。
雖然您可以復制和粘貼內容,但您需要手動調整頁面的設置。 在這種情況下,複製帖子或頁面是更自然和有效的選擇。
通過這樣做,您可以開始編輯帖子或頁面並立即開始處理。 此外,處理具有多個頁面的網站更容易。
現在的問題是,如何復制 WordPress 帖子或頁面?
如何復制 WordPress 帖子或頁面?
有三種方法可以復制 WordPress 帖子或頁面。 由於它是 WordPress,因此有一些插件可以為您完成這項工作。 如果您不想使用插件並想手動執行,您也可以選擇它。
使用插件
當您從您的網站複製頁面和帖子時,插件可確保安全和輕鬆。 它們與 WordPress 主題輕鬆集成,為您提供擴展功能。 最好的事情是,您不需要使用這些插件編寫一行代碼。
Yoast重複帖子
對於大多數 WordPress 用戶來說,Duplicate Post 可能是一個眾所周知的名字。 這是因為它是複制頁面和帖子的最受歡迎和首選選項。
您可以克隆所有內容,從內容到與頁面或帖子相關的評論。 要使用此插件,請按照以下步驟操作。
- 安裝並激活插件。
- 通過儀表板,轉到帖子 > 全部或頁面 > 全部。
- 導航到要復制的原始帖子/頁面,然後單擊克隆。
- 您可以使用批量操作一次克隆多個頁面。


重複頁面
與傳統插件相比,重複頁面是另一個出色的插件,具有多個附加功能。 您可以創建頁面、帖子和自定義帖子類型的副本。 創建副本後,您可以以任何所需的形式將其保存為草稿、公共、私人或待定。
請按照以下步驟使用該插件:
- 安裝並激活插件。
- 調整配置設置以滿足您的需求。
- 轉到頁面 > 全部或帖子 > 全部,然後選擇要復制的內容。
- 點擊複製這個,你就完成了。

重複頁面和帖子
這個插件是最輕量級和最快速的。 儘管它可能沒有其他插件那樣的高級功能,但它是最有效的插件之一。
要使用此插件複製您的帖子和頁面,請按照以下步驟操作:
- 安裝並激活插件。
- 轉到帖子/全部或頁面/全部。
- 打開要克隆的頁面或帖子。
- 點擊複製按鈕。


不使用插件
是的,插件很棒。 但這不是複制帖子和頁面的唯一方法。 您可以選擇自己手動完成,無需插件。 以下是您可以做到這一點的幾種方法。
通過functions.php代碼
您可以使用 functions.php 文件複製您的帖子和頁面。 將幾行代碼寫入現有的functions.php。 但是,請確保在更改 functions.php 文件之前徹底備份您的站點。
你需要對代碼有一些基本的了解才能理解它是如何工作的。 現在,複製並粘貼以下代碼:
/* * Function for post duplication. Dups appear as drafts. User is redirected to the edit screen */ function rd_duplicate_post_as_draft(){ global $wpdb; if (! ( isset( $_GET['post']) || isset( $_POST['post']) || ( isset($_REQUEST['action']) && 'rd_duplicate_post_as_draft' == $_REQUEST['action'] ) ) ) { wp_die('No post to duplicate has been supplied!'); } /* * Nonce verification */ if ( !isset( $_GET['duplicate_nonce'] ) || !wp_verify_nonce( $_GET['duplicate_nonce'], basename( __FILE__ ) ) ) return; /* * get the original post id */ $post_id = (isset($_GET['post']) ? absint( $_GET['post'] ) : absint( $_POST['post'] ) ); /* * and all the original post data then */ $post = get_post( $post_id ); /* * if you don't want current user to be the new post author, * then change next couple of lines to this: $new_post_author = $post->post_author; */ $current_user = wp_get_current_user(); $new_post_author = $current_user->ID; /* * if post data exists, create the post duplicate */ if (isset( $post ) && $post != null) { /* * new post data array */ $args = array( 'comment_status' => $post->comment_status, 'ping_status' => $post->ping_status, 'post_author' => $new_post_author, 'post_content' => $post->post_content, 'post_excerpt' => $post->post_excerpt, 'post_name' => $post->post_name, 'post_parent' => $post->post_parent, 'post_password' => $post->post_password, 'post_status' => 'draft', 'post_title' => $post->post_title, 'post_type' => $post->post_type, 'to_ping' => $post->to_ping, 'menu_order' => $post->menu_order ); /* * insert the post by wp_insert_post() function */ $new_post_id = wp_insert_post( $args ); /* * get all current post terms ad set them to the new post draft */ $taxonomies = get_object_taxonomies($post->post_type); // returns array of taxonomy names for post type, ex array("category", "post_tag"); foreach ($taxonomies as $taxonomy) { $post_terms = wp_get_object_terms($post_id, $taxonomy, array('fields' => 'slugs')); wp_set_object_terms($new_post_id, $post_terms, $taxonomy, false); } /* * duplicate all post meta just in two SQL queries */ $post_meta_infos = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$post_id"); if (count($post_meta_infos)!=0) { $sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) "; foreach ($post_meta_infos as $meta_info) { $meta_key = $meta_info->meta_key; if( $meta_key == '_wp_old_slug' ) continue; $meta_value = addslashes($meta_info->meta_value); $sql_query_sel[]= "SELECT $new_post_id, '$meta_key', '$meta_value'"; } $sql_query.= implode(" UNION ALL ", $sql_query_sel); $wpdb->query($sql_query); } /* * finally, redirect to the edit post screen for the new draft */ wp_redirect( admin_url( 'post.php?action=edit&post=' . $new_post_id ) ); exit; } else { wp_die('Post creation failed, could not find original post: ' . $post_id); } } add_action( 'admin_action_rd_duplicate_post_as_draft', 'rd_duplicate_post_as_draft' ); /* * Add the duplicate link to action list for post_row_actions */ function rd_duplicate_post_link( $actions, $post ) { if (current_user_can('edit_posts')) { $actions['duplicate'] = '<a href="' . wp_nonce_url('admin.php?action=rd_duplicate_post_as_draft&post=' . $post->ID, basename(__FILE__), 'duplicate_nonce' ) . '" title="Duplicate this item" rel="permalink">Duplicate</a>'; } return $actions; } add_filter( 'post_row_actions', 'rd_duplicate_post_link', 10, 2 );
如果你想克隆一個頁面而不是一個帖子,只需在代碼的最後一行添加一個頁面而不是一個帖子來做一個小的改動。

add_filter('page_row_actions', 'rd_duplicate_post_link', 10, 2);
手動複製和粘貼代碼
對於某些人來說,此過程可能會很累,因為它需要您從要克隆的每個頁面或帖子中復制並粘貼代碼。
請按照以下步驟手動克隆帖子或頁面:
- 打開要克隆的頁面或帖子。
- 轉到更多工具和選項菜單。
- 選擇代碼編輯器。
- 複製您要克隆的頁面或帖子的代碼。
- 單擊新帖子或新頁面。
- 在新頁面中打開代碼編輯器。
- 粘貼代碼。
- 轉到更多工具和選項菜單。
- 選擇可視化編輯器。
- 出現的頁面或帖子是舊頁面的克隆。
結論
關於如何復制 WordPress 帖子或頁面有很多選擇。 您也可以選擇使用插件或克隆您的頁面並在沒有它的情況下發布。 插件使您的工作更輕鬆。 同時,在沒有插件的情況下進行操作可以讓您完全控制事物。
因此,請使用最適合您需求的方法。 另外,請查看如何查找 WordPress 頁面 ID 和帖子 ID,如果我們的文章對您有幫助,請告訴我們。