<?php
$max = 7; // number item to get global $wpdb;
$sql = "SELECT c.*, p.post_title FROM
$wpdb->comments c INNER JOIN
$wpdb->posts p ON (c.comment_post_id=p.ID) WHERE comment_approved = '1'
AND comment_type not in ('trackback','pingback') ORDER BY comment_date DESC LIMIT $max";
$results = $wpdb->get_results($sql);
$template = '%g <a href="%au">%an</a> on <a href="%pu#comment-%cid">%pt</a>';
$echoed = 0; foreach ($results as $row)
{ $tags = array('%ct','%cd','%g','%pt','%pu','%au','%an','%cid');
$replacements = array($row->comment_title,
$row->comment_date,get_avatar($row->comment_author_email,'32'),
$row->post_title, get_permalink($row->comment_post_ID),
$row->comment_author_url,
$row->comment_author,
$row->comment_ID); echo '<li>' . str_replace($tags,$replacements,$template) . '</li>'; $echoed = 1;
}
if ($echoed==0)
echo '<li>No comment found.</li>';
?>
</ul>
§
<?php $sql = "SELECT c.*, p.post_title FROM $wpdb->comments c INNER JOIN
$wpdb->posts p ON (c.comment_post_id=p.ID)
WHERE comment_approved = '1' AND comment_type not in ('trackback','pingback')
ORDER BY comment_date DESC LIMIT $max"; $results = $wpdb->get_results($sql);
$template = '%g <a href="%au">%an</a> on <a href="%pu#comment-%cid">%pt</a>';
$echoed = 0; foreach ($results as $row)
{ $tags = array('%ct','%cd','%g','%pt','%pu','%au','%an','%cid');
$replacements = array($row->comment_title,$row->comment_date,
get_avatar($row->comment_author_email,'32'),
$row->post_title, get_permalink($row->comment_post_ID),
$row->comment_author_url,
$row->comment_author,
$row->comment_ID); echo '<li>' . str_replace($tags,$replacements,$template) . '</li>';
$echoed=1; } if ($echoed==0) echo '<li>No comment found.</li>'; ?> </ul>
§
管理員頻道
要為管理員提供特製訊息,我們可以利用 WPCandy 的程式碼段:
| <?php global $user_ID; if( $user_ID ) : if( current_user_can('level_10') ) : // admin-only stuff here. endif; endif; ?> |
在後臺的資訊中心,我們可以快速瀏覽到網站的總評論數、透過稽核的評論數以及待稽核的評論與垃圾評論。 現在我們可以用更方便的頁面模板來代替資訊中心的這個功能:
|
<?php $num_comm = wp_count_comments(); ?> <?php echo $num_comm->spam; ?> </a> |

wp_count_comments() 是一個可以返回多個評論統計數目的函式。 同時我們要為各個評論數字新增連結。
增加其它功能
最後,假設你找到一個很不錯的評論外掛,而你希望把這個外掛整合到頁面模板中。 這時無需新增其它程式碼,只要在頁面上加入對外掛的支援就可以了。 這裡我們以 Activity Sparks 外掛為例,Activity Sparks 可以 「在邊欄顯示文章和/或評論動態的曲線圖」 。
通常外掛帶有的 readme.txt 檔案都會指導使用者如何將外掛新增到主題檔案。 這個例子裡的程式碼如下:
|
<?php if(function_exists('activitysparks')) { activitysparks(array('dataset'=>'legend','height_px'=>100,'width_px'=>600,'period'=>30, 'ticks'=>24)); } ?> |

function_exists() 函式檢查特定函式是否可用,這裡即 activitysparks 函式。當外掛上傳並啟用成功時,activitysparks 函式函式可用。 如果函式可用,顯示曲線圖。 如果函式不可用,頁面模板不顯示內容 (但仍能夠正常執行) 。
結果演示
頁面模板的演示結果在這裡。 這個演示稍微修改了文章中提供的程式碼,主要為保證頁面 HTML 結構與網站其它部分一致。 Pastebin 上給出了頁面模板的完整程式碼。
原文:How to Create a Comments Central Page Template in WordPress (譯文)