admin管理员组文章数量:1122832
I can't find a snippet to show my comments in random order (not the posts, there's tons of entries for that one). I need this for my community page - I don't want them ordered by date. It's part of the concept of my site, that the comments are equally important and therefore should be shuffled before beeing displayed.
Could anyone provide me with a snippet for my functions.php and maybe even an explenation on how it works (I'm obviously not that good yet, but I might learn something :)
EDIT SINCE I FAIL HORRIBLY, here's my unedited comment.php:
<?php
/**
* The template for displaying comments.
*
* This is the template that displays the area of the page that contains both the current comments and the comment form.
*
* @link
*/
if (post_password_required()) {
return;
}
//Here?
foreach ( $comments as $comment ) :
echo $comment->comment_author . '<br />' . $comment->comment_content;
endforeach;
?>
<div id="comments" class="uk-margin-large-top">
<?php if (have_comments()) : ?>
<h2 class="uk-h3 uk-heading-bullet uk-margin-medium-bottom"><?php printf(_n(__('Comment'), __('Comments (%s)'), get_comments_number()), number_format_i18n(get_comments_number())) ?></h2>
<ul class="uk-comment-list"
//optional arguments in this array, without arguments will return all comments
$args = array();
$comments = get_comments( $args );
// use shuffle to randomize the order of the comments
shuffle($comments);>
<?php wp_list_comments([
'style' => 'ul',
'short_ping' => true,
'callback' => 'comment_callback',
]) ?>
</ul>
<?php if (get_comment_pages_count() > 1 && get_option('page_comments')) : ?>
<ul class="uk-pagination uk-flex-between">
<li class="nav-previous"><?php previous_comments_link('<span uk-pagination-previous></span> ' . __('Older Comments')) ?></li>
<li class="nav-next"><?php next_comments_link(__('Newer Comments') . ' <span uk-pagination-next></span>') ?></li>
</ul>
<?php endif ?>
<?php endif ?>
<?php if (!comments_open() && get_comments_number() && post_type_supports(get_post_type(), 'comments')) : ?>
<p class="uk-margin-medium uk-text-danger"><?php _e('Comments are closed.') ?></p>
<?php endif ?>
<?php get_template_part('commentform') ?>
</div>
<?php
/**
* The template to display a comment.
*/
function comment_callback($comment, $args, $depth) {
?>
<li id="comment-<?php comment_ID() ?>">
<article id="comment-article-<?php comment_ID() ?>" <?php comment_class('uk-comment uk-visible-toggle') ?> tabindex="-1">
<header class="uk-comment-header uk-position-relative">
<div class="uk-grid-medium uk-flex-middle" uk-grid>
<?php if ($args['avatar_size'] != 0) : ?>
<div class="uk-width-auto">
<?= get_avatar($comment, $args['avatar_size']) ?>
</div>
<?php endif ?>
<div class="uk-width-expand">
<h3 class="uk-comment-title uk-margin-remove"><?php comment_author_link($comment) ?></h3>
<p class="uk-comment-meta uk-margin-remove-top">
<a class="uk-link-reset" href="<?= esc_url(get_comment_link($comment, $args)) ?>">
<time datetime="<?php comment_time('c') ?>"><?php printf(__('%1$s at %2$s'), get_comment_date('', $comment), get_comment_time())
?></time>
</a>
</p>
</div>
</div>
<div class="uk-position-top-right uk-hidden-hover">
<?php comment_reply_link(array_merge($args, [
'depth' => $depth,
'max_depth' => $args['max_depth'],
'add_below' => 'comment-article',
])) ?>
</div>
</header>
<div class="uk-comment-body">
<?php if ($comment->comment_approved == '0') : ?>
<p><?php _e('Your comment is awaiting moderation.') ?></p>
<?php endif ?>
<?php comment_text() ?>
<?php edit_comment_link(__('Edit')) ?>
</div>
</article>
<?php
}
I can't find a snippet to show my comments in random order (not the posts, there's tons of entries for that one). I need this for my community page - I don't want them ordered by date. It's part of the concept of my site, that the comments are equally important and therefore should be shuffled before beeing displayed.
Could anyone provide me with a snippet for my functions.php and maybe even an explenation on how it works (I'm obviously not that good yet, but I might learn something :)
EDIT SINCE I FAIL HORRIBLY, here's my unedited comment.php:
<?php
/**
* The template for displaying comments.
*
* This is the template that displays the area of the page that contains both the current comments and the comment form.
*
* @link https://developer.wordpress.org/themes/basics/template-hierarchy
*/
if (post_password_required()) {
return;
}
//Here?
foreach ( $comments as $comment ) :
echo $comment->comment_author . '<br />' . $comment->comment_content;
endforeach;
?>
<div id="comments" class="uk-margin-large-top">
<?php if (have_comments()) : ?>
<h2 class="uk-h3 uk-heading-bullet uk-margin-medium-bottom"><?php printf(_n(__('Comment'), __('Comments (%s)'), get_comments_number()), number_format_i18n(get_comments_number())) ?></h2>
<ul class="uk-comment-list"
//optional arguments in this array, without arguments will return all comments
$args = array();
$comments = get_comments( $args );
// use shuffle to randomize the order of the comments
shuffle($comments);>
<?php wp_list_comments([
'style' => 'ul',
'short_ping' => true,
'callback' => 'comment_callback',
]) ?>
</ul>
<?php if (get_comment_pages_count() > 1 && get_option('page_comments')) : ?>
<ul class="uk-pagination uk-flex-between">
<li class="nav-previous"><?php previous_comments_link('<span uk-pagination-previous></span> ' . __('Older Comments')) ?></li>
<li class="nav-next"><?php next_comments_link(__('Newer Comments') . ' <span uk-pagination-next></span>') ?></li>
</ul>
<?php endif ?>
<?php endif ?>
<?php if (!comments_open() && get_comments_number() && post_type_supports(get_post_type(), 'comments')) : ?>
<p class="uk-margin-medium uk-text-danger"><?php _e('Comments are closed.') ?></p>
<?php endif ?>
<?php get_template_part('commentform') ?>
</div>
<?php
/**
* The template to display a comment.
*/
function comment_callback($comment, $args, $depth) {
?>
<li id="comment-<?php comment_ID() ?>">
<article id="comment-article-<?php comment_ID() ?>" <?php comment_class('uk-comment uk-visible-toggle') ?> tabindex="-1">
<header class="uk-comment-header uk-position-relative">
<div class="uk-grid-medium uk-flex-middle" uk-grid>
<?php if ($args['avatar_size'] != 0) : ?>
<div class="uk-width-auto">
<?= get_avatar($comment, $args['avatar_size']) ?>
</div>
<?php endif ?>
<div class="uk-width-expand">
<h3 class="uk-comment-title uk-margin-remove"><?php comment_author_link($comment) ?></h3>
<p class="uk-comment-meta uk-margin-remove-top">
<a class="uk-link-reset" href="<?= esc_url(get_comment_link($comment, $args)) ?>">
<time datetime="<?php comment_time('c') ?>"><?php printf(__('%1$s at %2$s'), get_comment_date('', $comment), get_comment_time())
?></time>
</a>
</p>
</div>
</div>
<div class="uk-position-top-right uk-hidden-hover">
<?php comment_reply_link(array_merge($args, [
'depth' => $depth,
'max_depth' => $args['max_depth'],
'add_below' => 'comment-article',
])) ?>
</div>
</header>
<div class="uk-comment-body">
<?php if ($comment->comment_approved == '0') : ?>
<p><?php _e('Your comment is awaiting moderation.') ?></p>
<?php endif ?>
<?php comment_text() ?>
<?php edit_comment_link(__('Edit')) ?>
</div>
</article>
<?php
}
Share
Improve this question
edited Apr 19, 2020 at 15:24
Beni
asked Apr 18, 2020 at 20:17
BeniBeni
112 bronze badges
2 Answers
Reset to default 1Never actually had a need for this but you will need to use the WP_Comment_Query function. There is this handy comments query generator online to customize this query easily.
But wait, it doesn't allow you to select a random order. No worries just add or replace the orderby parameter:
'orderby' => 'rand'
This should return comments ordered randomly. Let me know if you need any extra tips.
Edit: the above will not work.
I should have investigated deeper into the subject. The rand argument is not accepted in a comment query, as I thought it would, it is only accepted in post queries.
So how can you achieve this behavior. You can do it in an even simpler way using a built in PHP function to randomize arrays.
You should edit the theme's comment template, usually it is comments.php and replace the wp_list_comments() function with something like:
//optional arguments in this array, without arguments will return all comments
$args = array();
$comments = get_comments( $args );
// use shuffle to randomize the order of the comments
shuffle($comments);
Now you need to iterate the $comments array, and do something like:
foreach ( $comments as $comment ) :
echo $comment->comment_author . '<br />' . $comment->comment_content;
endforeach;
You can find more info on the get_comments() documentation page and the accepted arguments to customize the query.
The WP get_comments
function does not support ordering by random and will actually strip out any attempts to use arguments to do so.
This may be accomplished by using the comments_clauses
filter to adjust the orderby
statement passed to MySQL
.
add_filter( 'comments_clauses', function( array $clauses ) {
$clauses['orderby'] = ' RAND()';
return $clauses;
} );
This second requirement is to work around the WP_Comment_Query
internal caching which will return the same results repeatedly when the same arguments are passed to get_comments
.
You can handle this by passing a constantly changing value to one of the arguments. Since you must filter the orderby
value that seems like a good place to push something which changes.
/**
* Updates the cache key to get a new result once per second.
*
*/
get_comments( [ 'orderby' => (string) time() ] );
It is not recommended to do this on a high traffic area as there are some performance considerations:
- A new cache value will be stored in object cache on every run.
- A fresh comments query will run on every run.
But really, if the goal is get random results from MySQL every time it is called, then you need a fresh query every time anyway. Random ordered queries are non-performant by nature which is why some query types in WP don't support them.
本文标签: How can I show comments in random order
版权声明:本文标题:How can I show comments in random order? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736296100a1929713.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论