有没有什么方法可以通过共享帖子和模板等方式进行wordpress多域设置



我想知道是否有任何方法可以只有一个wordpress即时消息,它有两个不同的域(一个主域和一个子域(。域的内容应该不同,但也可以共享页面、模板和帖子。例如,一个页面";雇员";两个域都应该相同。

我已经建立了一个WordPress网络,所以多域设置可以工作,但不幸的是,页面、帖子和模板是分开的。有一些插件镜像了其他域上的帖子,但这里又是我保存了两次帖子的问题。

也许你已经有了一个插件,可以做到这一点,而不需要任何编码,但我不熟悉它们。

一个想法是挂接save_post操作,并将新创建的帖子推送到不同的博客。


if ( ! function_exists( 'post_to_multiple_sites' ) ) {
add_action( 'save_post', 'post_to_multiple_sites', 20, 2 );
function post_to_multiple_sites($original_id, $original_post) {

// To prevent publishing revisions
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $original_id;
}
// We only want to mess around with a post that has been published
if( 'publish' !== get_post_status( $original_post ) ) {
return $original_id;
}
// prevent "Fatal error: Maximum function nesting level reached"
remove_action( 'save_post', __FUNCTION__ );

/**
* If i'm correct, when creating a network, each site/blog receive an ID. 
* You can set these hardcoded, or create a function that returns an array of the blog id's.
* 
* If you only have a couple of sites, I would maintain this manually. Otherwise create a function to
* gather all the ID's and return it as an array (up to you).
*/
$blog_ids = [2, 3];     
$post_data = [
'post_author' => $original_post->post_author,
'post_date' => $original_post->post_date,
'post_modified' => $original_post->post_modified,
'post_content' => $original_post->post_content,
'post_title' => $original_post->post_title,
'post_excerpt' => $original_post->post_excerpt,
'post_status' => 'publish', // new post will be set to published
'post_name' => $original_post->post_name,
'post_type' => $original_post->post_type,
];

// Gather the post meta & terms
$post_terms = wp_get_object_terms( $original_id, 'category', array( 'fields' => 'slugs' ) );
$post_meta = get_post_custom( $original_id );
foreach ($blog_ids as $blog_id) {
switch_to_blog($blog_id);    // https://developer.wordpress.org/reference/functions/switch_to_blog/

// IN case a post with the same slug exists, don't do anything.
// Or maybe create a new post with slug-name-2...up to you.
if ( get_posts( [ 'name' => $post_data[ 'post_name' ], 'post_type' => $post_data[ 'post_type' ], 'post_status' => 'publish' ] ) ) {
restore_current_blog();
continue;
}
$inserted_post_id = wp_insert_post( $post_data );
wp_set_object_terms( $inserted_post_id, $post_terms, 'category', false );
foreach ( $post_meta as $meta_key => $meta_values) {
// we do not need these redirects
if( '_wp_old_slug' === $meta_key ) {
continue;
}
foreach ( $meta_values as $meta_value ) {
add_post_meta( $inserted_post_id, $meta_key, $meta_value );
}
}
restore_current_blog();
}
}
}

它变成了一个比我预期的更大的功能,老实说,我不知道你是否应该走这条路。你最安全的选择是尝试找到一个插件来为你处理这个问题。但你可以使用这段代码,并将其明显扩展到你的需求

请务必阅读我添加的评论。这些很重要

最新更新