评论作者url不执行任何操作

时间:2013-07-27 作者:Johan

我有这个功能:

<?php
function top_comment_authors($amount = 5) {
global $wpdb;
$results = $wpdb->get_results(\'
    SELECT
    COUNT(comment_author_email) AS comments_count, comment_author_email, comment_author, comment_author_url
    FROM \'.$wpdb->comments.\'
    WHERE comment_author_email != "" AND comment_type = "" AND comment_approved = 1
    GROUP BY comment_author_email
    ORDER BY comments_count DESC, comment_author ASC
    LIMIT \'.$amount
);

foreach($results as $result) {
    $output .= "<li><a href=".$result->comment_author_url.">".$result->comment_author."</a> (".$result->comments_count.")</li>";
}

echo $output;
}
?>
所以在这里,我试图链接到comment\\u author\\u url(作者页面),但它没有任何作用。怎么会这样?我做错了什么?

<li><a href=".$result->comment_author_url.">

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

将查询更改为:

SELECT
    COUNT(comment_author_email) AS comments_count, comment_author_email, comment_author, comment_author_url
    FROM \'.$wpdb->comments.\'
    WHERE comment_author_email != "" AND comment_type = "" AND comment_approved = 1
    GROUP BY comment_author_email
    ORDER BY comments_count DESC, comment_author ASC
    LIMIT \'.$amount
收件人:

SELECT *,
    COUNT(comment_author_email) AS comments_count, comment_author_email, comment_author, comment_author_url
    FROM \'.$wpdb->comments.\'
    WHERE comment_author_email != "" AND comment_type = "" AND comment_approved = 1
    GROUP BY comment_author_email
    ORDER BY comments_count DESC, comment_author ASC
    LIMIT \'.$amount
然后是:

$output .= "<li><a href=".$result->comment_author_url.">".$result->comment_author."</a> (".$result->comments_count.")</li>";
收件人:

$output .= "<li><a href=".get_author_posts_url($result->user_id).">".$result->comment_author."</a> (".$result->comments_count.")</li>";
修改查询将授予您访问所有字段的权限,其中一个字段(user\\u id)将用于获取作者的页面get_author_posts_url();

Update

我建议您在SQL字符串上使用双引号,在HTML上使用单引号。在您的代码中,您试图将$输出与任何内容连接起来。

因此,您的查询可能如下所示:

"
    SELECT *,
        COUNT(comment_author_email) AS comments_count, comment_author_email, comment_author, comment_author_url
        FROM $wpdb->comments
        WHERE comment_author_email != \'\' AND comment_type = \'\' AND comment_approved = 1
        GROUP BY comment_author_email
        ORDER BY comments_count DESC, comment_author ASC
        LIMIT $amount
"
然后对于$输出:

$output = \'<ul>\';
foreach($results as $result) {
$output .= \'<li><a href="\'.get_author_posts_url($result->user_id).\'">\'.$result->comment_author.\'</a> (\'.$result->comments_count.\')</li>\';
}
$output .= \'</ul>\';
希望有帮助!

结束

相关推荐

Custom Feed URLs

我正在为我的一个帖子类型创建一个自定义提要,并允许用户设置该提要的名称。http://www.mysite.com/feed/userdefinedname现在,我还想让用户不仅可以显示该CPT中的所有帖子,还可以按类别过滤它们。目前情况是这样的:http://www.mysite.com/feed/testcategory然而,如果我能将其结构如下,我会非常喜欢它:http://www.mysite.com/feed/userdefinedname/testcategory这可能吗?下面是生成这些基于类