您的问题的解决方案可以通过页面模板来完成,但首先我们需要修复您的预览代码
<?php
/**/
function add_user_page($user_id){
$user = get_userdata($user_id);
$role_with_page = "subscriber";
if( reset($user->roles) != $role_with_page ) return; //Exit if user role doesn\'t allows ownership of page
$post_type = \'page\'; /// I would prefer a custom post type like \'user_upages\'
global $wpdb;
$where = get_posts_by_author_sql( $post_type, true, $user_id );
if( $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" ) >= 1 ) return; // Exit if the user already have a page
$title = $user->user_login;
$template = \'user-profile.php\'; // if the post type where a CPT you could create just the file single-$posttype.php
$user_page = array(
\'post_title\' => $title,
\'post_type\' => $post_type,
\'post_content\' => \'\', // leave empty the post content so the user can edit this just in case than you will enable this option...
\'post_author\' => $user_id,
\'post_status\' => \'publish\',
\'post_theme\' => \'user-profile\', // ???? Are you talkin about page_template ?
\'page_template\' => $template
);
if( $post_id = wp_insert_post( $user_page ) ){
if( $post_type == \'page\' ) update_post_meta( $post_id, \'_wp_page_template\', $template );
__update_post_meta( $post_id, \'foobar\', \'some value\' ); // Save all the data you need as post_meta
__update_post_meta( $post_id, \'user_owner\', $user_id ); // This is just in case you dont want to give ownership (post_author) to the user
update_user_meta( $user_id, \'user_owns_unique_page\', $post_id ); // Store the page owned by this user
update_user_meta( $user_id, \'some_random_stuff\', rand( 0,10 ) ); // Store the page owned by this user
}
//do_action(\'acf/save_post\' , $post_id); /* Uncomment this if you use ACF (than you should) ;) */
return $post_id;
}
add_action( \'user_register\', \'add_user_page\' );
add_action( \'set_user_role\', \'add_user_page\' );
?>
您应该保持post\\u内容的原样,因为这是静态数据,不会检索用户数据。如果您真的想更新post\\u内容,那么只需添加一个短代码(您应该在functions.php中编写短代码)。例如:。
\'post_content\' => "[show_user_data id=\'{$user_id}\']",
然后,如果要使用page\\u模板,请使用您的用户配置文件。php应该像这样:
<?php
/*
* Template Name: User profile page
* Description: A Page Template for users.
*/
get_header();
while ( have_posts() ) :
the_post();
$user_id = get_the_author_meta( "ID" ); // or get_post_meta( get_the_ID(), \'user_owner\', true );
$user = get_userdata( $user_id );
$user_name = $user->first_name." ".$user->last_name;
$some_random_stuff = get_user_meta( $user_id, \'some_random_stuff\', $single );
$foobar = get_post_meta( get_the_ID() , \'foobar\', true );
?>
<ul>
<li><?php echo $user_name ?></li>
<li><?php echo $some_random_stuff ?></li>
<li><?php echo $foobar ?></li>
<li><?php the_content(); ?></li>
</ul>
<?php
endwhile;
get_footer();
?>
您可以阅读以下内容:
https://codex.wordpress.org/Page_Templates希望这有帮助。