如何輕鬆複製 WordPress 頁面/發布
已發表: 2021-11-05
在許多情況下,複製頁面或帖子的現有內容可能很有用。 您可以使用主要帖子/頁面作為模板并快速完成您的任務。 如果您必須重新創建同一頁面/帖子的內容,您能想像會花費多少時間嗎?
好吧,我無法想像。 值得慶幸的是,有一些方法可以在 WordPress 頁面/帖子中復制內容。 在這篇文章中,我將向您展示如何立即復制 WordPress 頁面/帖子。
讓我們開始吧。
- 為什麼要復制 WordPress 頁面/帖子?
- 使用插件在 WordPress 中重複內容
- 沒有插件的WordPress重複頁面/帖子
- 最後的話
為什麼要復制 WordPress 頁面/帖子?
當您在頁面或帖子中進行一些新更改時,您可能需要快速克隆頁面/帖子。
例如,您有一個包含很多部分的銷售頁面; 現在您需要為不同的產品再次創建相同的銷售頁面,您會怎麼做? 重新創建整個頁面?
當然不是。 您將能夠通過複製獲得完全相同的頁面。 您將能夠立即開始工作並節省時間。 您可以通過兩種方式複制 WordPress 頁面/帖子 - 1. 使用插件,2. 不使用插件。
使用插件在 WordPress 中重複內容
有幾個插件,例如 - Duplicate Page、Post Duplicator、Duplicate page and Post、Page 和 Any Custom Post; 這可以幫助您立即復制 WordPress 頁面/發布。 但最受歡迎的是 Yoast Duplicate Post。
首先,從 WordPress 插件目錄安裝並激活 Yoast Duplicate Post 插件。
在儀表板中,轉到發布 -> 所有發布。 (要復制頁面,請轉到頁面 -> 所有頁面)
現在將光標懸停在任何帖子上,您將看到兩個新選項——“克隆和新草稿”。
現在單擊克隆按鈕以復制帖子。
但是,如果您想創建一個克隆並立即在帖子編輯器中打開它,請單擊“新建草稿”按鈕。
沒有插件的WordPress重複頁面/帖子
如果不想增加已安裝插件的數量,可以按照這個方法。 這很簡單,您只需將一些代碼複製到 function.php 文件中,然後您就可以非常輕鬆地複制 WordPress 頁面/帖子。
注意:在編輯任何 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( 'post_row_actions', 'rd_duplicate_post_link', 10, 2 );
不要忘記更新文件。
上面的代碼適用於復制帖子。 如果要復制頁面,只需將最後一行替換為以下代碼:
add_filter('page_row_actions', 'rd_duplicate_post_link', 10, 2);
最後的話
我希望現在你有一種簡單的方法來複製 WordPress 頁面/帖子。 是否使用插件都沒有關係; 您將以兩種方式獲得相同的結果。 如果您有任何問題或建議,請通過下面的評論框告訴我。