Commented entry list 2.0をWordpressで利用しているのですが、コメントを入れてもらうと親タイトルの下に時間(日)と投稿者の名前がつきますが、このコメントに対してレスを入れると次の投稿者は先の人の上に記載されます。このプラグインを開いてみてDESCからASCに変更してみたら昇順にはなり2番目の投稿者は最初の投稿者の下に記載されるようになるのですが、親タイトルまでが昇順になり一番したに落ちてしまいます。
そこでご教授お願いしたいのは、投稿者の記載が常に先の人の下にまわり、なおかつ親タイトルは一番新しいコメントがついているものが一番上にくるようにしたいと考えております。
私の力では自己解決不能のためご教授お願いしたく、投稿いたしました。
Commented entry list 2.0 のソースは下のアドレスにUPしてあります。
http://www.luckymails.net/list.txt
以上宜しくお願い申しあげます。 こんにちは、また宜し..
コメントでのやりとりも含めて、get_recently_commented() のコードを整理しました。
<?php ... function get_recently_commented($limit = 10) { global $wpdb; $tablecomments = $wpdb->comments; $tableposts = $wpdb->posts; //★ここから //* $comments = $wpdb->get_results("SELECT ID, post_title, post_date, comment_ID, comment_author, comment_author_url, comment_author_email, comment_date FROM $tableposts, $tablecomments WHERE $tableposts.ID=$tablecomments.comment_post_ID AND $tablecomments.comment_approved='1' AND $tablecomments.comment_type='' ORDER BY $tablecomments.comment_date ASC LIMIT $limit"); // SQL の LIMIT の範囲を、最後の $limit 件にする $n_comments = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $tablecomments WHERE $tablecomments.comment_approved='1' AND $tablecomments.comment_type='' ;")); $limit_start = $n_comments - $limit + 1; $comments = $wpdb->get_results("SELECT ID, post_title, post_date, comment_ID, comment_author, comment_author_url, comment_author_email, comment_date FROM $tableposts, $tablecomments WHERE $tableposts.ID=$tablecomments.comment_post_ID AND $tablecomments.comment_approved='1' AND $tablecomments.comment_type='' ORDER BY $tablecomments.comment_date ASC LIMIT $limit_start, $n_comments"); //★ここまで $output = ''; if (is_array($comments) && count($comments) == 0) { echo "No Responses."; } else { //★ここから //* foreach ($comments as $comment) { //* $posts[$comment->ID]++; //* } // $ports をコメントID 毎のコメント数ではなく、コメントの最新の日付を持つ連想配列にする foreach ($comments as $comment) { if (array_key_exists($comment->ID, $posts)) { if (strcmp($comment->comment_date, $posts[$comment->ID])) { $posts[$comment->ID] = $comment->comment_date; } } else { $posts[$comment->ID] = $comment->comment_date; } } // ports を comment_date の逆順で並べ替え arsort($posts); //★ここまで while (list ($key, $val) = each($posts)) { $first_comment = TRUE; foreach ($comments as $comment) { if ($comment->ID != $key) continue; if ($first_comment == TRUE) { $post_title = stripslashes($comment->post_title); $permalink = get_permalink($comment->ID)."#commentlist"; $output .= "<li>"; $output .= "<a href=\"$permalink\">$post_title</a>\n"; $output .= "\t<ul>\n"; $first_comment = FALSE; } $comment_date = $comment->comment_date; $commentid = get_permalink($comment->ID)."#comment-".$comment->comment_ID; if ( mysql2date('Y m d', $comment_date) == gmdate('Y m d', current_time(timestamp)) ) { # Today's comment $comment_date = mysql2date('H:i', $comment_date); } else { $comment_date = mysql2date('m/d', $comment_date); } $output .= "\t\t<li><span class=\"comment_date\"><a href=\"$commentid\" title=\"\">$comment_date</a> </span>". "<span class=\"comment_author\">".tkzy_get_comment_author_link($comment,20)."</span></li>\n"; } $output .= "\t</ul>\n</li>\n"; } echo $output; } } ... ?>
質問のリンク先にあった、元のコードは //* でコメントアウトして、残してあります。
$limit は、10 のままです。
SQL の並べ替えは、comment_date の昇順 (ASC) です。
# きちんと動くかな >ドキドキ
get_recently_commented() の以下のくだりを、
foreach ($comments as $comment) {
$posts[$comment->ID]++;
}
というふうに変更したら、期待通りになりません?
// $ports をコメントID 毎のコメント数ではなく、コメントの最新の日付を持つ連想配列にする foreach ($comments as $comment) { if (array_key_exists($comment->ID, $posts)) { if (strcmp($comment->comment_date, $posts[$comment->ID])) { $posts[$comment->ID] = $comment->comment_date; } } else { $posts[$comment->ID] = $comment->comment_date; } } // ports を comment_date の逆順で並べ替え arsort($posts);
ぼくは、あまり php が得意じゃないんで、初歩的なミスがあるかもしれませんが m(_ _)m
コメントでのやりとりも含めて、get_recently_commented() のコードを整理しました。
<?php ... function get_recently_commented($limit = 10) { global $wpdb; $tablecomments = $wpdb->comments; $tableposts = $wpdb->posts; //★ここから //* $comments = $wpdb->get_results("SELECT ID, post_title, post_date, comment_ID, comment_author, comment_author_url, comment_author_email, comment_date FROM $tableposts, $tablecomments WHERE $tableposts.ID=$tablecomments.comment_post_ID AND $tablecomments.comment_approved='1' AND $tablecomments.comment_type='' ORDER BY $tablecomments.comment_date ASC LIMIT $limit"); // SQL の LIMIT の範囲を、最後の $limit 件にする $n_comments = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $tablecomments WHERE $tablecomments.comment_approved='1' AND $tablecomments.comment_type='' ;")); $limit_start = $n_comments - $limit + 1; $comments = $wpdb->get_results("SELECT ID, post_title, post_date, comment_ID, comment_author, comment_author_url, comment_author_email, comment_date FROM $tableposts, $tablecomments WHERE $tableposts.ID=$tablecomments.comment_post_ID AND $tablecomments.comment_approved='1' AND $tablecomments.comment_type='' ORDER BY $tablecomments.comment_date ASC LIMIT $limit_start, $n_comments"); //★ここまで $output = ''; if (is_array($comments) && count($comments) == 0) { echo "No Responses."; } else { //★ここから //* foreach ($comments as $comment) { //* $posts[$comment->ID]++; //* } // $ports をコメントID 毎のコメント数ではなく、コメントの最新の日付を持つ連想配列にする foreach ($comments as $comment) { if (array_key_exists($comment->ID, $posts)) { if (strcmp($comment->comment_date, $posts[$comment->ID])) { $posts[$comment->ID] = $comment->comment_date; } } else { $posts[$comment->ID] = $comment->comment_date; } } // ports を comment_date の逆順で並べ替え arsort($posts); //★ここまで while (list ($key, $val) = each($posts)) { $first_comment = TRUE; foreach ($comments as $comment) { if ($comment->ID != $key) continue; if ($first_comment == TRUE) { $post_title = stripslashes($comment->post_title); $permalink = get_permalink($comment->ID)."#commentlist"; $output .= "<li>"; $output .= "<a href=\"$permalink\">$post_title</a>\n"; $output .= "\t<ul>\n"; $first_comment = FALSE; } $comment_date = $comment->comment_date; $commentid = get_permalink($comment->ID)."#comment-".$comment->comment_ID; if ( mysql2date('Y m d', $comment_date) == gmdate('Y m d', current_time(timestamp)) ) { # Today's comment $comment_date = mysql2date('H:i', $comment_date); } else { $comment_date = mysql2date('m/d', $comment_date); } $output .= "\t\t<li><span class=\"comment_date\"><a href=\"$commentid\" title=\"\">$comment_date</a> </span>". "<span class=\"comment_author\">".tkzy_get_comment_author_link($comment,20)."</span></li>\n"; } $output .= "\t</ul>\n</li>\n"; } echo $output; } } ... ?>
質問のリンク先にあった、元のコードは //* でコメントアウトして、残してあります。
$limit は、10 のままです。
SQL の並べ替えは、comment_date の昇順 (ASC) です。
# きちんと動くかな >ドキドキ
コメント(8件)
foreach ($comments as $comment) {
$posts[$comment->ID]++;
}
を消して下のコードを入れてみたのですが、変化ありませんでした。
やり方間違ってないですよね。
そのつもりで書きました。
$ports が、キーにコメントID で、値にコメント数を持ってたところを、
キーは同じで、値に、そのコメントID で一番新しいコメント日付を持たせて、
コメント日付の逆順に並べ替えてるつもりなんですけどねえ...
function get_recently_commented($limit = 20) {
global $wpdb;
$tablecomments = $wpdb->comments;
$tableposts = $wpdb->posts;
$comments = $wpdb->get_results("SELECT ID, post_title, post_date, comment_ID, comment_author, comment_author_url, comment_author_email, comment_date FROM $tableposts, $tablecomments WHERE $tableposts.ID=$tablecomments.comment_post_ID AND $tablecomments.comment_approved='1' AND $tablecomments.comment_type='' ORDER BY $tablecomments.comment_date DESC LIMIT $limit");
この部分の最後の行のDESCをASCに変えると親タイトルも最上部にあがり投稿者も下に下にいくようになり希望どおりになるのですが・・・
コメントの件数を20件に設定している状態で次に投稿したコメントが21件目だとしたらこの21件目の親タイトルも投稿者も表示されません。上限の20を30に変更すれば最上部に新しい親タイトルと投稿者が表示されます。
この部分の修正をお願い出来ればと思います。欲を言えば投稿者の件数でカウントしているようですが、親タイトルで件数を指定出来れば嬉しいのですが複雑になるのであれば無視してください。
お手数お掛けして申し訳ありません。
質問にあったリンク先が order by ... ASC になってたので、それを想定してました。
>次に投稿したコメントが21件目だとしたらこの21件目の親タイトルも投稿者も表示されません。
最初に、COUNT(*) を使って、条件に合致するコメント数をカウントし、
その値から件数を引いた値を指定して、LIMIT を範囲指定すれば良いんじゃないでしょうか?
$n_comments = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $tablecomments WHERE $tablecomments.comment_approved='1' AND $tablecomments.comment_type='' ;"));
$limit_start = $n_comments - $limit + 1;
... = get_results("SELECT ... LIMIT $limit_start, $n_comments");
$limit_start = $n_comments - $limit + 1;
... = get_results("SELECT ... LIMIT $limit_start, $n_comments");
たびたび申し訳ございません。ど素人なもんですから再度質問いたします。
上のコードは、一番最初の回答の
arsort($posts);
の後ろに挿入すればいいのでしょうか?
また、...の部分は何を入れればいいでしょうか?
ほんまに恐縮してしまいますが、宜しくお願いいたします。
たびたびなのは、別にかまわないんですけど、
ぼくも、素人に毛が生えた程度なので、ドキドキしながら答えてるんよ (^^;
>ほんまに恐縮してしまいますが、宜しくお願いいたします。
恐縮するこたぁありません。
そういう趣旨のサイトです >人力検索
ありがたいお言葉と数度にわたるご教授を頂きましてありがとうございます。
最終のコードを張り付けたところ希望どおりになりました。
ほんまにお世話になりありがとうございました。
こっちも、php は素人に毛が生えた程度なので、逆に、紆余曲折して答えにたどりついたところも、
参考になったかな、なんて。
きっと、php が得意な他の人たちは、ハラハラして見てたに違いない (^^;