[英]display “be the first to comment on a post” message if no comments are made php mysqli
[英]Display first comment of post outside the comments template (using SQL maybe)
在Wordpress中,如何在注释表之外查找并显示第一个注释或管理员的注释?
我想象像一个显示该帖子的第一条评论的sql查询...
有什么线索吗? 谢谢!
此功能可获取所有帖子的5条最近评论。 我想对此进行编辑,并显示我正在阅读的最新文章
function showLatestComments() {
global $wpdb;
$sql = "
SELECT DISTINCT comment_post_ID, comment_author, comment_date_gmt, comment_approved, SUBSTRING(comment_content,1,100) AS com_excerpt
FROM $wpdb->comments
WHERE comment_approved = '1'
ORDER BY comment_date_gmt DESC
LIMIT 5";
$comments = $wpdb->get_results($sql);
$output .= '<h2>latest comments</h2><ul id="comm">';
foreach ($comments as $comment) {
$output .= '<li><strong>'. $comment->comment_author . ' said</strong> : "' . strip_tags($comment->com_excerpt). '..."</li>';
}
$output .= '</ul>';
echo $output;
}//end function
我认为“第一篇文章”是最早的评论。 一种简单的方法是更改顺序(因此,前面的注释会优先出现),然后移至第一行。
$sql = "
SELECT DISTINCT comment_post_ID, comment_author, comment_date_gmt,
comment_approved, SUBSTRING(comment_content,1,100) AS com_excerpt
FROM $wpdb->comments
WHERE comment_approved = '1'
ORDER BY comment_date_gmt ASC
LIMIT 1";
如果您这样做,仅需要更改这两行。
ORDER BY comment_date_gmt ASC
LIMIT 1";
我对WordPress的数据库结构不熟悉,但基本上,您将必须获取当前帖子的ID,并将其作为WHERE参数传递。
我假设comment_post_ID跟踪添加评论的帖子的ID? 如果是这样的话:
$id = mysql_escape_string($_GET["id"]); // get id of post somewhere
$sql = "
SELECT DISTINCT comment_post_ID, comment_author, comment_date_gmt, comment_approved, SUBSTRING(comment_content,1,100) AS com_excerpt
FROM $wpdb->comments
WHERE comment_approved = '1' AND comment_post_ID = {$id}
ORDER BY comment_date_gmt DESC
LIMIT 5";
这将显示您通过ID传递的5条最新评论
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.