2012-09-03 75 views
3

我想要建立一個頁面,將顯示所有評論,無論他們附加到哪個帖子。我也希望該頁面可以分頁,因爲它可能有超過10,000條評論。WordPress的獨立評論頁 - 與分頁


我不知道如何去了解它,但這裏有一些至今我研究的功能:

  1. get_comments - 如果沒有post_id傳遞中,它將返回所有評論。然而,我沒有看到一種方法來分頁這些(有offsetnumber選項來擺弄,但這是繁瑣的手動)。

  2. wp_list_comments - 這個文檔是非常糟糕的,但the source code表明,我們可以遍歷所有的意見,如果配合使用get_comments,由get_comments數組中傳遞的第二個參數。然而,這實際上仍然會使用get_comments ......好吧,得到評論,似乎沒有辦法對此進行分頁。

  3. previous_comments_link & next_comments_link - 這些似乎與wp_list_comments結合唯一的工作(沒有第二個參數)。

  4. paginate_comments_links - 看起來它只適用於wp_list_comments(沒有第二個參數)。


我已經試過什麼:

  1. 只要在get_commentsnumber說法:

    $comments = get_comments(array(
        'status' => 'approve', 
        'number' => '2' 
    )); 
    
    wp_list_comments(array(
        'callback' => 'my_rendering_function' 
    ), $comments); 
    
    paginate_comments_links(); 
    

    這並不顯示任何分頁鏈接。

  2. 的方法這裏建議:Display latest comments on page with pagination

    $comments = get_comments(array(
        'status' => 'approve' 
    )); 
    
    wp_list_comments('per_page=2', $comments); 
    
    paginate_comments_links(); 
    

    這也不起作用(它顯示第2個評論,但無分頁)。另外,我在get_comments加載全部注意到內存。


問:

我怎麼能分頁所有評論?


P.S.我使用WordPress 3.4.1 & PHP 5.3.2。

+0

我沒有看到一個直接的方式來做到這一點,你到底需要利用'offset'和'number'來獲得需要的評論。看看SEO pager插件,你可以得到分頁功能,或者使用自定義WordPress循環來實現你的功能。 –

+0

@ UmeshAwasthi - 你讀過[鏈接到帖子](http://wordpress.stackexchange.com/questions/7413/display-latest-comments-on-page-with-pagination)嗎?看來應該有辦法做到這一點。我誤解了什麼嗎? – MegaHit

回答

3

如果您打算建立自己的分頁,您需要知道您要使用的評論總數,因此您將不得不載入所有評論。

我已經建立了我要使用的東西,讓我知道它是否有幫助。

#Config here. 
define('DEFAULT_COMMENTS_PER_PAGE',100); 

$page = (int) (!isset($_REQUEST["page"]) ? 1 : $_REQUEST["page"]); 
$limit = DEFAULT_COMMENTS_PER_PAGE; 
$offset = ($page * $limit) - $limit; 
$param = array(
    'status'=>'approve', 
    'offset'=>$offset, 
    'number'=>$limit, 
); 
$total_comments = get_comments(array('status'=>'approve')); 
$pages = ceil(count($total_comments)/DEFAULT_COMMENTS_PER_PAGE); 
$comments = get_comments($args); 
foreach($comments as $comment) { 
    // ECHO THE AUTHOR AND COMMENT 
    echo("<strong>{$comment->comment_author}</strong> - {$comment->comment_content}"); 
} 
$args = array(
'base'   => '%_%', 
'format'  => '?page=%#%', 
'total'  => $pages, 
'current'  => $page, 
'show_all'  => False, 
'end_size'  => 1, 
'mid_size'  => 2, 
'prev_next' => True, 
'prev_text' => __('&laquo; Previous'), 
'next_text' => __('Next &raquo;'), 
'type'   => 'plain'); 
// ECHO THE PAGENATION 
echo paginate_links($args); 
+0

只是爲了提高我以前的答案,你不需要物理地獲取所有的評論,但你至少應該知道你在哪裏。 EG 25一個頁面,你可能想要顯示類似1,2,3,4,...所以你需要知道你是否有足夠的評論來填補這些網頁,或者如果你必須提前停止。簡單地獲得4x25會幫助你找出這些信息。如果你想知道總頁數,你仍然需要知道評論總數。 – PsyKzz

0

我們應該擔心使用get_comments()獲得總體評論的性能。必須使用參數偏移量爲false,並計爲true,如下所示:

$comments_per_page = 2; 
$page = 2; 

//MYSQL: LIMIT offset, number 
$params = array(
    'post_id' => $post_id, 
    'offset' => (--$page) * $comments_per_page, 
    'number' => $comments_per_page, 
); 

$comments = get_comments($params); 
$total_comments = get_comments(
    array_merge($params, 
      array(
       'count' => true, 
       'offset' => 0, 
       'number' => 0 
      ) 
    ) 
); 


$args = array(
    'total'  => ceil($total_comments/$comments_per_page), 
    'current' => max(1, $page), 
); 

$pagination = paginate_links($args); 

添加您需要的任何參數。你可以使用wp_list_comments()作爲WEEL,但如果你想自定義輸出HTML,使用的foreach爲$評論:

foreach ($comments as $comment) : 

    echo $comment->comment_date; 
    echo '<br>'; 
    echo $comment->comment_author; 
    echo '<br>'; 
    echo $comment->comment_content; 

endforeach;