我找不到一个片段以随机顺序显示我的评论(不是帖子,有很多条目)。我的社区页面需要这个-我不希望他们按日期排序。这是我的网站概念的一部分,评论同样重要,因此在展示之前应该进行洗牌。
谁能给我一个函数的代码片段吗。php,甚至可以解释它是如何工作的(我显然还不是很好,但我可能会学到一些东西:)
EDIT SINCE I FAIL HORRIBLY, 这是我未经编辑的评论。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
}
SO网友:Patareco
从未真正需要过,但您需要使用WP_Comment_Query 作用有个手边的comments query generator 联机以轻松自定义此查询。
但是等等,它不允许您选择随机顺序。无需担心,只需添加或替换orderby参数:
\'orderby\' => \'rand\'
这将返回随机排序的注释。如果你需要额外的小费,请告诉我。
Edit: the above will not work.
我本应该深入研究这个问题的。rand参数在注释查询中不被接受,正如我所想,它只在post查询中被接受。
那么,您如何实现这种行为呢。您可以使用内置的PHP函数随机化数组,以一种更简单的方式完成此操作。
您应该编辑主题的注释模板,通常是注释。php并将wp\\u list\\u comments()函数替换为如下内容:
//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);
现在,您需要迭代$comments数组,并执行如下操作:
foreach ( $comments as $comment ) :
echo $comment->comment_author . \'<br />\' . $comment->comment_content;
endforeach;
您可以在
get_comments() documentation page 和
the accepted arguments to customize the query.
<小时>