如何在 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 网站获取新主题,请不要错过它们!