我试图在自定义区域显示特定用户的评论。我成功地做到了这一点,但问题是permalink结构。这段代码将url设置为禁用永久链接,如果我从设置中启用永久链接,那么这些url将开始给出404。
以下是im使用的代码:
<?
if(get_query_var(\'author_name\')) :
$curauth = get_userdatabylogin(get_query_var(\'author_name\'));
else :
$curauth = get_userdata(get_query_var(\'author\'));
endif;
$querystr = "
SELECT comment_ID, comment_post_ID, post_title, comment_content
FROM $wpdb->comments, $wpdb->posts
WHERE user_id = $uid
AND comment_post_id = ID
AND comment_approved = 1
ORDER BY comment_ID DESC LIMIT 5
";
$comments_array = $wpdb->get_results($querystr, OBJECT);
if ($comments_array): ?>
<ul>
<? foreach ($comments_array as $comment):
setup_postdata($comment);
echo "<li><a href=\'". get_bloginfo(\'url\') ."/?p=".$comment->comment_ID."\'>Comment on ". $comment->post_title. "</a><br />". $comment->comment_content . "</li>";
endforeach; ?>
</ul>
<? endif; ?>
所以这个url是www.mysite。com/p=xxxx。。如果永久链接作为mysite启用。com/post-perma-links/it开始在通过上述代码生成的这些链接上给出404。函数的其余部分工作正常,
在这种情况下,有可能获得永久链接url而不是p=xxx?谢谢你的帮助干杯
最合适的回答,由SO网友:MikeSchinkel 整理而成
你好@Ayaz Malik:
您需要使用该函数get_comment_link()
. 我已经使用一些改进的技术重写了您的代码,并包含了函数调用来代替您所拥有的:
global $wpdb;
$sql =<<<SQL
SELECT
{$wpdb->comments}.comment_ID,
{$wpdb->comments}.comment_post_ID,
{$wpdb->comments}.comment_content,
{$wpdb->posts}.post_title
FROM
{$wpdb->comments}
INNER JOIN {$wpdb->posts}
ON {$wpdb->comments}.comment_post_id={$wpdb->posts}.ID
WHERE 1=1
AND {$wpdb->comments}.user_id = %d
AND {$wpdb->comments}.comment_approved = 1
ORDER BY
{$wpdb->comments}.comment_ID DESC
LIMIT 5
SQL;
$sql = $wpdb->prepare($sql,$uid); // $uid is assumed pre-defined before this code
$comments = $wpdb->get_results($sql, OBJECT);
if ($comments) {
echo \'<ul>\';
foreach ($comments as $comment) {
setup_postdata($comment);
$link = get_comment_link($comment->comment_ID);
echo "<li><a href=\'{$link}\'>Comment on {$comment->post_title}</a><br />" .
"{$comment->comment_content}</li>";
}
echo \'</ul>\';
}