我使用交易评论,因此每条评论都会收到多个回复。我需要得到每个评论的回复计数。我不需要所有评论的总回复数。
我尝试使用此代码:
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if(adapter != null){
adapter.getItem(position).onResume();
}
Toast.makeText(getApplicationContext(), "Page Selected", Toast.LENGTH_LONG).show();
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
另一种方式显示总回复而不是我正在寻找的内容..此代码显示所有评论的所有回复。
functions.php中的代码
$comment_id = get_comment_ID();
$childrenCount = get_comment_meta( $comment_id, "childrenCount" );
$childrenCountNumber = count($childrenCount);
echo $childrenCountNumber
然后在模板页面中回显:
function replies_counter($id){
global $wpdb;
$query = "SELECT COUNT(comment_post_id) AS count FROM $wpdb->comments
WHERE `comment_approved` = 1 AND `comment_post_ID` = $id AND
`comment_parent` = 0";
$parents = $wpdb->get_row($query);
return $parents->count;
}
这一个是关于获得所有评论的总回复数。但我需要单独回复每条评论的数量。
如果有人帮助我会很好。
答案 0 :(得分:1)
我总是喜欢使用WordPress核心功能,如果有一个可以做我想要的。
您可以使用get_comments轻松实现这一目标。将count
arg设置为true
。这样它只会检索注释计数。
$args = array(
'post_id' => $post->ID, //main post id
'parent' => get_comment_ID(), //the comment id
'count' => true, //just count
);
$comments = get_comments($args); //number of comments;
答案 1 :(得分:0)
为什么不编写这样的自定义函数来按ID获取每个评论的子评论?
function subreplies_counter($id){
global $wpdb;
$query = "SELECT COUNT(comment_ID) AS count FROM $wpdb->comments
WHERE `comment_approved` = 1 AND `comment_parent` = $id";
$data = $wpdb->get_row($query);
return $data->count;
}
答案 2 :(得分:0)
这是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://codex.wordpress.org/Template_Hierarchy
*
* @package Astra
* @since 1.0.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/*
* If the current post is protected by a password and
* the visitor has not yet entered the password we will
* return early without loading the comments.
*/
if ( post_password_required() ) {
return;
}
?>
<div id="comments" class="comments-area">
<h3>{<?php echo get_comments_number(); ?><span style="color:grey;font-size:16px"> comments</span>}</h3>
<?php astra_comments_before(); ?>
<?php if ( have_comments() ) : ?>
<div class="comments-count-wrapper">
<h2 class="comments-title">
<?php
$comments_title = apply_filters(
'astra_comment_form_title',
sprintf( // WPCS: XSS OK.
/* translators: 1: number of comments */
esc_html( _nx( '%1$s thought on “%2$s”', '%1$s thoughts on “%2$s”', get_comments_number(), 'comments title', 'astra' ) ),
number_format_i18n( get_comments_number() ),
get_the_title()
)
);
echo esc_html( $comments_title );
?>
</h2>
</div>
<?php if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) : ?>
<nav id="comment-nav-above" class="navigation comment-navigation" aria-label="<?php esc_attr_e( 'Comments Navigation', 'astra' ); ?>">
<h3 class="screen-reader-text"><?php echo esc_html( astra_default_strings( 'string-comment-navigation-next', false ) ); ?></h3>
<div class="nav-links">
<div class="nav-previous"><?php previous_comments_link( astra_default_strings( 'string-comment-navigation-previous', false ) ); ?></div>
<div class="nav-next"><?php next_comments_link( astra_default_strings( 'string-comment-navigation-next', false ) ); ?></div>
</div><!-- .nav-links -->
</nav><!-- #comment-nav-above -->
<?php endif; ?>
<ol class="ast-comment-list">
<?php
wp_list_comments(
array(
'callback' => 'astra_theme_comment',
'style' => 'ol',
)
);
?>
</ol><!-- .ast-comment-list -->
<?php if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) : ?>
<nav id="comment-nav-below" class="navigation comment-navigation" aria-label="<?php esc_attr_e( 'Comments Navigation', 'astra' ); ?>">
<h3 class="screen-reader-text"><?php echo esc_html( astra_default_strings( 'string-comment-navigation-next', false ) ); ?></h3>
<div class="nav-links">
<div class="nav-previous"><?php previous_comments_link( astra_default_strings( 'string-comment-navigation-previous', false ) ); ?></div>
<div class="nav-next"><?php next_comments_link( astra_default_strings( 'string-comment-navigation-next', false ) ); ?></div>
</div><!-- .nav-links -->
</nav><!-- #comment-nav-below -->
<?php endif; ?>
<?php endif; ?>
<?php
// If comments are closed and there are comments, let's leave a little note, shall we?
if ( ! comments_open() && get_comments_number() && post_type_supports( get_post_type(), 'comments' ) ) :
?>
<p class="no-comments"><?php echo esc_html( astra_default_strings( 'string-comment-closed', false ) ); ?></p>
<?php endif; ?>
<?php comment_form(); ?>
<?php astra_comments_after(); ?>
</div><!-- #comments -->