0

我的问题是,如何在 wordpress 的页面上显示随机评论?在我的网站上,我有一个 wp 页面,人们在其中留下很多评论,我希望它们随机显示,而不是按日期时间分类,这是分页评论的代码,我该怎么办?谢谢 :)

<?php foreach ($comments as $comment) : ?>

        <li <?php echo $oddcomment; ?>id="comment-<?php comment_ID() ?>">
            <div class="paginated-comments-number" style="float: left; color: #999; width: 30px; text-align: left;"></div>
        <?php
                if ( function_exists('get_avatar') )
                    echo get_avatar( $comment, 48 );
            ?>
            <cite><?php comment_author_link() ?> 
            </cite>
            <?php if ($comment->comment_approved == '0') : ?>
            <em>Your comment is awaiting moderation.</em>
          <?php endif; ?><br />

            <small class="commentmetadata"><a href="<?php echo Paginated_Comments_URL('comment-' . get_comment_ID()); ?>" title=""><?php comment_date('F jS, Y') ?> at <?php comment_time() ?></a> <?php edit_comment_link('edit','&nbsp;&nbsp;',''); ?></small>

            <?php comment_text() ?>
        </li>

    <?php
        /* Changes every other comment to a different class */
        $oddcomment = ( empty( $oddcomment ) ) ? 'class="alt" ' : '';
    ?>

    <?php endforeach; /* end for each comment */ ?>
</ol>

<p>
      <!-- Start Paginated Comments Pages -->
      <?php if ( Paginated_Comments_have_pages() ) : ?>
</p>
    <p>
      <?php endif; ?>
      <!-- End Paginated Comments Pages -->

      <?php else : // this is displayed if there are no comments so far ?>

      <?php if ('open' == $post->comment_status) : ?>
      <!-- If comments are open, but there are no comments. -->

      <?php else : // comments are closed ?>
4

1 回答 1

0

下面是一个代码功能的建议,可以满足您的要求:

add_shortcode( 'randomComment', 'randomComment_handler' );

function randomComment_handler($post_id) {
    extract( shortcode_atts( array(
        'post_id' => '0',
        ), $atts ) );
    $out = "";
    $comments = get_comments("post_id=$post_id&status=approve");
    if ($comments) {
        $ndx = mt_rand(0,sizeof($comments)) - 1; 
        $comment = $comments[$ndx];
        $out = "<div class='randomComment'><div class='randomAuthor'>".$comment->comment_author."</div><div class='randomText'>".$comment->comment_content."</div></div>";
    }
    return $out;
}

这进入您的functions.php, 并允许您将简码放在任何页面或帖子上以显示随机评论:

[randomComment post_id="1337"]

只需post_id根据您要从中提取随机评论的帖子更改。

于 2012-08-03T06:32:24.210 回答