如何将用户限制在每个帖子只能有一条评论

时间:2014-03-13 作者:cmsdeployed

我使用WordPress作为请愿网站,并入侵评论系统作为请愿注册。我想了解如何限制用户每篇文章只能发表一条评论。到目前为止,我可以限制每个用户在整个WordPress网站上发表一条评论,如下面的代码所示,但这不是我想要实现的。

同样,我如何将每个用户的每条帖子限制为一条评论?

   <?php
   /*
    * Single Petition Template
    * @package WordPress
    * @subpackage Twenty_Thirteen
    * @since Twenty Thirteen 1.0
    * template author: Facinet Toure
    * website URL: http://mongage.com
   */

 get_header(); ?>

 <div id="primary" class="content-area">
  <div id="content" class="site-content" role="main">
    <?php /* The loop */ ?>
    <?php while ( have_posts() ) : the_post(); ?>
    <?php //get_template_part( \'content\', get_post_format() ); ?>
    <!-- post format -->
    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
        <header class="entry-header">
            <?php if ( has_post_thumbnail() && ! post_password_required() ) : ?>
            <div class="entry-thumbnail">
                <?php the_post_thumbnail(\'full\'); ?>
            </div>
            <?php endif; ?>
            <h1 class="entry-title">
                <?php the_title(); ?>
            </h1>
            <p><span class="rounded-corners alignleft"><?php echo get_avatar( $post->post_author, 74 ) ?></span>
                <span class="author"><?php the_author_meta( \'display_name\' ); ?></span>
                <span> | </span>This post currently has
                <?php comments_number( \'no responses\', \'one response\', \'% responses\' ); ?>
                .</p>
        </header>
        <!-- .entry-header -->
        <div class="entry-content">
            <?php the_content( __( \'Continue reading <span class="meta-nav">&rarr;</span>\', \'twentythirteen\' ) ); ?>
            <?php wp_link_pages( array( \'before\' => \'<div class="page-links"><span class="page-links-title">\' . __( \'Pages:\', \'twentythirteen\' ) . \'</span>\', \'after\' => \'</div>\', \'link_before\' => \'<span>\', \'link_after\' => \'</span>\' ) ); ?>
        </div>
        <!-- .entry-content -->

        <footer class="entry-meta">
            <?php twentythirteen_entry_meta(); ?>
            <?php if ( comments_open() && ! is_single() ) : ?>
            <span class="comments-link">
            <?php comments_popup_link( \'<span class="leave-reply">\' . __( \'Sign the petition\', \'twentythirteen\' ) . \'</span>\', __( \'One comment so far\', \'twentythirteen\' ), __( \'View all % comments\', \'twentythirteen\' ) ); ?>
            </span><!-- .comments-link -->
            <?php endif; // comments_open() ?>
            <?php edit_post_link( __( \'Edit\', \'twentythirteen\' ), \'<span class="edit-link">\', \'</span>\' ); ?>
            <?php if ( is_single() && get_the_author_meta( \'description\' ) && is_multi_author() ) : ?>
            <?php get_template_part( \'author-bio\' ); ?>
            <?php endif; ?>
        </footer>
        <!-- .entry-meta --> 
    </article>
    <!-- #post -->

    <p> This post currently has
        <?php comments_number( \'no responses\', \'one response\', \'% responses\' ); ?>
        . </p>
    <?php twentythirteen_post_nav(); ?>
    <ol reversed class="commentlist">
        <?php
    //Gather comments for a specific page/post 
    $comments = get_comments(array(
        \'post_id\' => $post->ID,
        \'status\' => \'approve\' //Change this to the type of comments to be displayed
    ));

    //Display the list of comments
    wp_list_comments(array(
        \'per_page\' => -1, //Allow comment pagination
        \'reverse_top_level\' => false, //Show the latest comments at the top of the list,
        \'max_depth\' => \'1\',
        \'avatar_size\' => 0,
    ), $comments);
?>
    </ol>
    <?php // check if user has previously commented the post
        global $current_user;
        if ( !is_user_logged_in() ) {
        echo \'you must be logged in to sign the petition\';
        } else {
        $args = array(\'user_id\' => $current_user->ID);
        $usercomment = get_comments($args);
        if(count($usercomment) >= 1) { 
            echo \'<p>Vous avez déjà signé cette pétition. S\\\'il vous plaît partager avec votre famille et vos amis</p>\';

        } else { comment_form(); 
        } 
    }?>
    <?php endwhile; ?>
   </div>
   <!-- #content --> 
   </div>
  <!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

1 个回复
最合适的回答,由SO网友:cmsdeployed 整理而成

// Check if user has previously commented the post.
global $current_user, $post;

if ( ! is_user_logged_in() ) {
    // Show the comment form if the user is not logged in.
    comment_form();
} else { // The user is logged in...

    // Get the comments for the logged in user.
    $usercomment = get_comments( array (
            \'user_id\' => $current_user->ID,
            \'post_id\' => $post->ID,
    ) );

    // If the user has commented, output a message.
    if ( $usercomment ) { 
        echo \'<p>Vous avez déjà signé cette pétition. S\\\'il vous plaît partager avec votre famille et vos amis</p>\';
    } else { // Otherwise, show the comment form.
        comment_form(); 
    }
}
现在,代码正在检查用户之前是否对该特定帖子而不是整个站点发表了评论。希望其他人会觉得它有帮助。

结束

相关推荐

如何从插件文件中调用Comments_Template();?

我正在打电话comments_template(); 从插件文件。具体来说player.php 插件中的文件All Video Gallery. 下面是文件本身。<?php require_once(\'ismobile.php\'); /****************************************************************** /* Build Player ***********************