WordPress - 允许对作者页面发表评论

Zac*_*ous 6 php wordpress comments

我需要能够允许用户在作者个人资料页面上发表评论.

我在这里找到了另一个答案,它给了我一个基本的概述,告诉我需要做些什么来实现它:https://wordpress.stackexchange.com/questions/8996/author-page-comments-and-ratings

也就是说,我不确定如何继续实施.我创建了一个自定义帖子类型来保存评论,但我不知道如何制作它以便每次用户/作者注册我们的网站(我们正在建立一个开放注册的网站)时,会创建一个帖子用于保存注释的自定义帖子类型,然后自动与其用户配置文件关联.

非常感谢比链接问题提供的更详细的答案,以便我能够准确理解如何启动和运行.

非常感谢

Ana*_*kar 3

实际上你需要简单地将东西与user_register操作挂钩,例如

function my_user_register($user_id){

    $user = get_user_by( 'id', $user_id );

    /**
     * if required you can limit this profile creation action to some limited 
     * roles only
     */

    /**
     * Created new Page under "user_profile_page" every time a new User 
     * is being created
     */

    $profile_page = wp_insert_post(array(
        'post_title'        => " $user->display_name Profile ", // Text only to Map those page @ admin
        'post_type'         => "user_profile_page", // Custom Post type which you have created 
        'post_status'       => 'publish',
        'post_author'       => $user_id,
    ));

    /**
     * Save the Profile Page id into the user meta
     */
    if(!is_wp_error($profile_page))
        add_user_meta($user_id,'user_profile_page',$profile_page,TRUE);
}

/**
 * Action which is being trigger Every time when a new User is being created
 */
add_action('user_register','my_user_register');
Run Code Online (Sandbox Code Playgroud)

上面的代码是您在原始帖子中实际上缺少的东西https://wordpress.stackexchange.com/questions/8996/author-page-comments-and- ratings 所以在添加上面的代码之后,您只需简单地遵循author.php与该代码相同

$profile_page = get_the_author_meta('user_profile_page');

global $post;
$post = get_post($profile_page);

setup_postdata( $post ); 

//fool wordpress to think we are on a single post page
$wp_query->is_single = true;
//get comments
comments_template();
//reset wordpress to ture post
$wp_query->is_single = false;

wp_reset_query();
Run Code Online (Sandbox Code Playgroud)