IT技术 · 2022年3月29日

如何在 WordPress 中复制/克隆页面或文章

当我们需要大部分相同的格式、标签、分类、自定义字段、SEO 数据等时,我们需要复制页面或文章功能。在本教程中,我们将学习如何在 WordPress 中复制页面或文章。我们可以使用插件或不使用插件来做到这一点。

因此,我们将在文章列表或页面列表页面上创建一个锚链接,当我们单击该链接时,它将创建一个具有相同内容的新文章并将其保存为草稿。

但是如果没有复制功能,您必须一一检查所有字段并制作另一个新文章。

让我们开始吧!

使用插件复制页面或文章

使用插件可以很容易地复制 WordPress 文章或页面。因为它为您提供了所有功能,您只需单击操作链接即可克隆页面。

下面就是我们推荐的几个好用的文章复制插件:

1. Duplicate Page

WordPress文章复制插件 Duplicate Page

Duplicate Page插件可以让您可以一键复制您的页面、文章和自定义文章类型的文章,它会保存为您选择的选项(草稿、私人、公开、待定)。

2. Duplicate Page and Post

WordPress文章复制插件 Duplicate Page and Post

Duplicate Page and Post插件是一个非常轻量级的插件,它是最快的复制器。它提供了创建页面或文章的克隆的功能。

3. Post Duplicator

WordPress文章复制插件 Post Duplicator

Post Duplicator插件将选中的文章或页面完全相同的副本。它可以使文章、页面和自定义文章类型重复,并且支持自定义分类法和自定义字段。

4. Yoast Duplicate Post

WordPress文章复制插件 Yoast Duplicate Post

Yoast Duplicate Post 插件允许用户克隆任何类型的文章,或将它们复制到新草稿中以供进一步编辑。在“编辑帖子”/“编辑页面”中,您可以单击帖子/页面标题下方的“克隆”链接:这将立即创建一个副本并将其返回到列表中。

通过代码添加复制文章的功能

在此方法中,我们将创建复制 WordPress 页面或文章的功能。我们将在functions.php文件中添加一个代码片段。

注意:我建议您使用子主题在您的网站上进行任何自定义,或者使用 Code Snippets 管理代码片段

添加复制功能

/*
* Function creates post duplicate as a draft and redirects then to the edit post screen
*/
function duplicate_post_or_page(){
	global $wpdb;
	if (! ( isset( $_GET['post']) || isset( $_POST['post'])  || ( isset($_REQUEST['action']) && 'duplicate_post_or_page' == $_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();
	$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'    => $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); 
		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 = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$post_id");
		if (count($post_meta)!=0) {
			$sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) ";
			foreach ($post_meta 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_duplicate_post_or_page', 'duplicate_post_or_page' );
上面的代码将创建一个具有相同内容(如标签、分类、自定义字段等)的文章。因此,将此代码添加到主题的 functions.php 文件中并保存。

在文章列表添加复制链接

上面我们制作了复制 WordPress 文章或页面的功能,并将新文章作为具有相同数据的草稿。而下面的代码将在列表页面上一个新的动作,运行上面的功能。因此,在主题的functions.php文件中添加以下代码并保存。

/*
 * Add the duplicate link to the action list for post_row_actions
 */
function duplicate_post_link( $actions, $post ) {
	if (current_user_can('edit_posts')) {
		$actions['duplicate'] = '<a href="'%20.%20wp_nonce_url('admin.php?action=duplicate_post_or_page&post='%20.%20%24post->ID,%20basename(__FILE__),%20'duplicate_nonce'%20)%20.%20'" title="Duplicate this item" rel="permalink">Duplicate</a>';
	}
	return $actions;
}
 
add_filter( 'post_row_actions', 'duplicate_post_link', 10, 2 );
在主题的functions.php文件中添加两个代码块后,您将在帖子列表页面上看到一个新的操作链接。标签将是Duplicate,您可以更改为克隆或复制。
WordPress 文章复制

现在您可以通过单击复制操作链接来克隆您的 WordPress 文章。它将创建具有相同内容的新文章,并将您重定向到编辑帖子页面。您可以在此处发布或起草。

但是,如果您还想复制 WordPress 页面怎么办?

别担心!您不需要为此创建额外的功能。我们将使用与上述相同的功能,并将仅添加一个过滤器挂钩线来运行上述页面并显示页面的重复链接。

在页面列表添加复制链接

在添加了上面【 在文章列表添加复制链接 】的代码基础上,只需再将下面的代码添加到主题的functions.php 文件中,就可以为页面显示相同的复制链接。

add_filter('page_row_actions', 'duplicate_post_link', 10, 2);

效果如下所示:

WordPress 页面复制

总结

在本教程中,我们介绍了使用插件或代码实现文章/页面一键复制的功能,希望对大家有所帮助。如果您有任何问题,请在下面评论。

最新电影,电视剧,尽在午夜剧场

电影电视剧午夜不寂寞