如何在 WordPress 中正確複製頁面(有 2 種方法)

已發表: 2022-09-23

不要浪費時間手動複製 WordPress 站點中的每個頁面。 如果您打算複製許多頁面,讓我們向您展示 2 個在 WordPress 中以簡單且省時的方式複制頁面的解決方案。

什麼時候需要在 WordPress 中復制頁面?

有時,您需要在 WordPress 中復制頁面以在相同的頁面佈局中展示不同的內容。 您可以手動執行此操作,這意味著您必須複製模板和 SEO 元素,包括元描述和標題標籤。 這個過程會重複很多次,這需要你的時間和精力。

如果您需要另一種更方便、更省時但仍然高效的解決方案,建議您通過 plugins 或 functions.php 文件在 WordPress 中復制頁面。 今天,我們想為您帶來詳細的說明。 讓我們開始吧!

如何在 WordPress 中復制頁面

實際上,在 WordPress 中復制頁面有兩種主要方法。 第一個是使用 WordPress Duplicator Plugins 來做到這一點。 另一種是在functions.php文件中添加代碼以生成重複頁面甚至重複帖子。

但是,我們建議您應該選擇一個插件來實現您的目標。 將代碼添加到主題文件編輯器有時會導致您的 WordPress 網站出錯,並且一切都變得複雜。 因此,獲得安全的解決方案對您來說是個好主意。

使用插件複製 WordPress 中的頁面

老實說,在當前市場上,似乎有許多方便的插件可以幫助您在 WordPress 中復制頁面。 但是,我們想介紹兩個易於使用且有效的插件:Yoast Duplicate Post 和 Duplicate Page and Post。

現在,讓我們看一下為您的 WordPress 網站使用Yoast Duplicate Post的詳細說明。

  • 登錄到您的 WordPress 管理儀表板 -> 轉到插件 -> 添加新的
  • 找到Yoast Duplicate Post -> 安裝並激活它。
  • 在管理儀表板上,選擇Open Page -> All Pages
  • 選擇您要復制的頁面,將有 2 個新選項:克隆新草稿
  • 如果您希望在 WordPress 中復制頁面,只需單擊克隆按鈕。 如果您需要生成包含複製內容的新頁面,讓我們選擇新建草稿選項。

Just Yoast Duplicate Post to Duplicate pages in Wordpress

另一個幫助您克隆當前頁面的插件是Duplicate Page 和 Post 。 讓我們按照以下步驟操作:

  • 登錄到您的 WordPress 管理儀表板 -> 轉到插件 -> 添加新的
  • 搜索重複頁面並發布-> 安裝並激活它。
  • 訪問打開頁面 -> 所有頁面
  • 單擊您需要復制的頁面,將出現“複製”選項供您執行此操作。 按下它後,將有一個新的草稿供您編輯內容。

在 Wordpress 中使用重複頁面和發布來重複頁面

使用 Functions.php 文件複製 WordPress 中的頁面

在 WordPress 中復制頁面的第二種方法是將代碼插入到 Functions.php 文件中。 為此,讓我們打開Appearance -> Theme File Editor -> Functions.php

通過 Functions.php 在 Wordpress 中復制頁面

現在,您可以通過將以下代碼片段添加到 functions.php 文件來複製 WordPress 中的頁面:

/*
* 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('page_row_actions', 'rd_duplicate_post_link', 10, 2);

包起來

簡而言之,讓我們按照上面的兩種方法之一在 WordPress 中復制頁面來節省克隆頁面的時間。 如果您有任何與主題相關的問題,為什麼不在下面留下您的評論,以便我們為您提供支持?

最後但並非最不重要的一點是,不要忘記我們的市場提供了許多適合移動設備且引人注目的免費 WordPress 主題。 因此,如果您需要為您的 WordPress 網站獲取新主題,請不要錯過它們!