【问题标题】:WordPress - Allowing comments on author pagesWordPress - 允许在作者页面上发表评论
【发布时间】:2015-12-09 11:48:09
【问题描述】:

我需要能够允许用户在作者个人资料页面上留下 cmets。

我在这里遇到了另一个答案,它让我大致了解了需要做些什么来实现它:https://wordpress.stackexchange.com/questions/8996/author-page-comments-and-ratings

也就是说,我不确定如何继续实施。我创建了一个自定义帖子类型来保存 cmets,但我不知道如何制作,以便每次用户/作者注册我们的网站(我们正在构建一个开放注册的网站)时,都会创建一个帖子用于保存 cmets 的自定义帖子类型,然后自动关联到他们的用户个人资料。

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

非常感谢

【问题讨论】:

    标签: php wordpress comments


    【解决方案1】:

    实际上你只需要将 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');
    

    上面的代码实际上是你在原始帖子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();
    

    【讨论】:

    • 非常感谢您的全面回答。我现在已经实现并运行了! :)
    【解决方案2】:

    您可以使用 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; 
    }
    ?>
    

    【讨论】:

      【解决方案3】:

      您可以使用以下代码更新您的 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。如果您遇到任何其他问题,请告诉我。

      【讨论】:

        猜你喜欢
        • 2012-01-19
        • 2023-03-13
        • 1970-01-01
        • 1970-01-01
        • 2016-06-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-18
        相关资源
        最近更新 更多