获取特定类别的最新评论

时间:2013-01-29 作者:Sam Hanson

我正在使用get_comments(\'status=approve&number=5\') 在我的wordpress侧边栏中显示最近的评论。我需要显示特定类别的最近评论。如何做到这一点?

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

在里面this link, 以下代码可解决您的问题:

// Posts per page setting
$ppp = get_option(\'posts_per_page\'); // either use the WordPress global Posts per page setting or set a custom one like $ppp = 10;
$custom_offset = 0; // If you are dealing with your custom pagination, then you can calculate the value of this offset using a formula

// category (can be a parent category)
$category_parent = 3;

// lets fetch sub categories of this category and build an array
$categories = get_terms( \'category\', array( \'child_of\' => $category_parent, \'hide_empty\' => false ) );
$category_list =  array( $category_parent );
foreach( $categories as $term ) {
 $category_list[] = (int) $term->term_id;
}

// fetch posts in all those categories
$posts = get_objects_in_term( $category_list, \'category\' );

$sql = "SELECT comment_ID, comment_date, comment_content, comment_post_ID
 FROM {$wpdb->comments} WHERE
 comment_post_ID in (".implode(\',\', $posts).") AND comment_approved = 1
 ORDER by comment_date DESC LIMIT $ppp OFFSET $custom_offset";

$comments_list = $wpdb->get_results( $sql );

if ( count( $comments_list ) > 0 ) {
 $date_format = get_option( \'date_format\' );
 echo \'<ul>\';
 foreach ( $comments_list as $comment ) {
 echo \'<li>Comment: \'.substr( $comment->comment_content, 0, 50 ).\'..<br />\'.date( $date_format, strtotime( $comment->comment_date ) ).\'<br />Post: <a href="\'.get_permalink( $comment->comment_post_ID ).\'">\'.get_the_title( $comment->comment_post_ID ).\'</a></li>\';
 }
 echo \'</ul>\';
} else {
 echo \'<p>No comments</p>\';
}
?>

结束

相关推荐

如何正确使用Comments-template.php

我想修改已登录用户和未登录用户的评论表单。我通过更改注释修改了未登录用户的表单。php,但我不太确定如何为登录用户修改我的表单。我知道我必须使用comments\\u template(),但每当我尝试在页面中使用它时。php,我遇到这样的错误Notice: Undefined variable: args in {PATH}/twwr-theme/comments-template.php on line 13 这些是我评论的内容。php,有点乱<?php if (!empty