WordPress - 允许在作者页面上发表评论
WordPress - Allowing comments on author pages
我需要能够允许用户在作者个人资料页面上发表评论。
我在这里遇到了另一个答案,它给了我一个基本的轮廓,说明需要做什么才能允许它:https://wordpress.stackexchange.com/questions/8996/author-page-comments-and-ratings
也就是说,我不确定如何着手实施它。我已经创建了一个自定义 post 类型来保存评论,但我不知道如何做到每次 user/author 注册我们的网站(我们正在建立一个开放注册的网站) , 在自定义 post 类型中创建 post 来保存评论,然后自动关联到他们的用户个人资料。
非常希望能得到比链接问题中提供的更详细的答案,这样我才能准确理解如何解决这个问题,运行。
非常感谢
您可以使用 insert_user_meta 挂钩将新过滤器挂接到 wp_insert_user 函数(如果您使用它在注册步骤中插入用户),因此,在创建新用户后,此过滤器将在保存之前在元数组上调用。以下是可能有用的示例代码:
<?php
add_filter('insert_user_meta', 'nl_associate_cp_to_user', 10, 3);
function nl_associate_cp_to_user($meta, $user, $update ){
$meta['cpid_for_comments'] = nl_get_new_cpid_for_comments();
return $meta;
}
function nl_get_new_cpid_for_comments(){
$data = array(
'post_type' => 'CCPT',
'post_title' => 'Stub Post',
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
);
$post_id = wp_insert_post( $data );
if(!is_wp_error($post_id)) return $post_id;
}
?>
实际上,您只需将 Stuff 与 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');
上面的代码实际上是你在原来的代码中遗漏的东西 post 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();
您可以使用以下代码更新您的 user-author.php 文件。
<?php
//save blog Id
$blog_id = $post->ID;
// now map your post with author post
$author_post_id = get_user_meta($user_id, author_post_id,blog);
query_posts("p=$author_post_id");
the_post();
//Your are in a single post page
$wp_query->is_single = blog;
//get comments
comments_template();
//reset wordpress to particular post
$wp_query->is_single = false;
query_posts("p=$blog_id");
the_post();
?>
您需要了解这里我们假设特定博客的博客 ID。如果您遇到任何其他问题,请告诉我。
我需要能够允许用户在作者个人资料页面上发表评论。
我在这里遇到了另一个答案,它给了我一个基本的轮廓,说明需要做什么才能允许它:https://wordpress.stackexchange.com/questions/8996/author-page-comments-and-ratings
也就是说,我不确定如何着手实施它。我已经创建了一个自定义 post 类型来保存评论,但我不知道如何做到每次 user/author 注册我们的网站(我们正在建立一个开放注册的网站) , 在自定义 post 类型中创建 post 来保存评论,然后自动关联到他们的用户个人资料。
非常希望能得到比链接问题中提供的更详细的答案,这样我才能准确理解如何解决这个问题,运行。
非常感谢
您可以使用 insert_user_meta 挂钩将新过滤器挂接到 wp_insert_user 函数(如果您使用它在注册步骤中插入用户),因此,在创建新用户后,此过滤器将在保存之前在元数组上调用。以下是可能有用的示例代码:
<?php
add_filter('insert_user_meta', 'nl_associate_cp_to_user', 10, 3);
function nl_associate_cp_to_user($meta, $user, $update ){
$meta['cpid_for_comments'] = nl_get_new_cpid_for_comments();
return $meta;
}
function nl_get_new_cpid_for_comments(){
$data = array(
'post_type' => 'CCPT',
'post_title' => 'Stub Post',
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
);
$post_id = wp_insert_post( $data );
if(!is_wp_error($post_id)) return $post_id;
}
?>
实际上,您只需将 Stuff 与 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');
上面的代码实际上是你在原来的代码中遗漏的东西 post 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();
您可以使用以下代码更新您的 user-author.php 文件。
<?php
//save blog Id
$blog_id = $post->ID;
// now map your post with author post
$author_post_id = get_user_meta($user_id, author_post_id,blog);
query_posts("p=$author_post_id");
the_post();
//Your are in a single post page
$wp_query->is_single = blog;
//get comments
comments_template();
//reset wordpress to particular post
$wp_query->is_single = false;
query_posts("p=$blog_id");
the_post();
?>
您需要了解这里我们假设特定博客的博客 ID。如果您遇到任何其他问题,请告诉我。