如何轻松复制 WordPress 页面/发布

已发表: 2021-11-05
wordpress 中的重复内容

在许多情况下,复制页面或帖子的现有内容可能很有用。 您可以使用主要帖子/页面作为模板并快速完成您的任务。 如果您必须重新创建同一页面/帖子的内容,您能想象会花费多少时间吗?

好吧,我无法想象。 值得庆幸的是,有一些方法可以在 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 页面/帖子。 是否使用插件都没有关系; 您将以两种方式获得相同的结果。 如果您有任何问题或建议,请通过下面的评论框告诉我。