WordPressは「最近の投稿」、「最近のコメント」がデフォルト機能にない。プラグインもあるようだったが、この程度ならということで、ソース解析してみたら、比較的ラクに(期待どおりの)ハックが可能でした。
最近の投稿はデフォルトのメソッドで表示可能。
◆wp-content/themes/EasyAll/sidebar.php
<li><h2><?php echo mb_convert_encoding('最近の投稿', 'UTF-8', 'AUTO');?></h2>
<ul>
<?php
wp_get_archives('type=postbypost&limit=10&format=html');
?>
</ul>
</li>
コメントは、ハックが必要。
◆wp-includes/general-template.php
411行目付近
wp_get_archives(‘type=commentbypost&limit=10&format=html’);
のように呼び出せるようにコード追加。
※postbypostを参考にDBからwp_commentsテーブルを参照し、適宜必要なカラムを抽出する。
適切なSQLで
$arcresults = $wpdb->get_results
が実行された後であれば、
$arcresults→カラム名 で値参照可能なのでDB構造がわかっていればラク。
411 } elseif ( ( 'commentbypost' == $type ) || ('alpha' == $type) ) {
412 ('alpha' == $type) ? $orderby = "comment_date ASC " : $orderby = "comment_date DESC ";
413 $arcresults = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_type != 'trackback' AND comment_approved = '1' ORDER BY $orderby $limit");
414 if ( $arcresults ) {
415 foreach ( $arcresults as $arcresult ) {
416 if ( $arcresult->comment_date != '0000-00-00 00:00:00' ) {
417 $url = '?p=' . $arcresult->comment_post_ID . '#comments';
418 //コメントした人のIDは、$arcresult->comment_author
419 //コメントした日付は、 $arcresult->comment_date
420 $arc_title = mb_substr($arcresult->comment_content, 0, 10) . ' ...';
421 if ( $arc_title )
422 $text = strip_tags($arc_title);
423 else
424 $text = $arcresult->ID;
425 echo get_archives_link($url, $text, $format, $before, $after);
426 }
427 }
428 }
429 }
430 }
◆wp-content/themes/EasyAll/sidebar.php
<li><h2><?php echo mb_convert_encoding('最近のコメント', 'UTF-8', 'AUTO');?></h2>
<ul>
<?php
wp_get_archives('type=commentbypost&limit=10&format=html');
?>
</ul>
</li>
No comments yet. You should be kind and add one!
By submitting a comment you grant typista a perpetual license to reproduce your words and name/web site in attribution. Inappropriate and irrelevant comments will be removed at an admin’s discretion. Your email is used for verification purposes only, it will never be shared.