2017-10-27 6 views
1

ユーザーが作成者ページで行ったコメントの一覧をページネーションリンクと共に表示しようとしています。Wordpress Comment 404という結果のページ番号付きクエリ

最初に私はfunctions.phpに単純な関数を作成して、著者のコメント数を得ました。 var_dump(commentCount())author.phpで返します26

次でその特定のユーザのコメントの正確な数を行う

function commentCount() { 
    global $wpdb; 
    $author = get_user_by('slug', get_query_var('author_name')); 
    $count = $wpdb->get_var('SELECT COUNT(comment_ID) FROM ' . $wpdb->comments. ' WHERE comment_author_email = "' . $author->user_email . '"'); 
    return $count; 
} 

、私は著者のコメントの内容を取得するためにWP_Comment_Query()を使用してカスタムクエリを作成しました。

$comments_per_page = 10; 

/*Count comments*/ 
$all_comments_approved = commentCount(); 

/*Get Current Page Var*/ 
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 

/*How many comments offset*/ 
$offset = (($paged-1) * $comments_per_page) ; 

/*Max number of pages*/ 
$max_num_pages = ceil($all_comments_approved/$comments_per_page); 

$args = array(
    'user_id' => $author->ID, // comments by this user only 
    'status' => 'approve', 
    'post_status' => 'publish', 
    'post_type' => 'post', 
    'number' => $comments_per_page, 
    'offset' => $offset 
); 

$comments_query = new WP_Comment_Query; 
$comments = $comments_query->query($args); 

// Comment Loop 
if ($comments) { 
    foreach ($comments as $comment) { 
    echo $comment->comment_content . '<br>'; 
    } 

    /*Set current page for pagination*/ 
    $current_page = max(1, get_query_var('paged')); 

    /*Echo paginate links*/ 
    echo paginate_links(array(
    'base' => get_pagenum_link(1) . '%_%', 
    'current' => $current_page, 
    'total' => $max_num_pages, 
    'prev_text' => __('&laquo; Previous'), 
         'next_text' => __('Next &raquo;'), 
         'end_size' => 2, 
         'mid-size' => 3 
        )); 
} else { 
    echo 'No comments found.'; 
} 

上記のコードはauthor.phpに配置され、それは、コメントを取得し、$comments_per_page指定として1日あたり10を表示します。そして最後にページネーションのリンクを見ることができます、私は3ページを参照してください。

2番目のページに移動すると、他に10件のコメントが表示されます。私は3ページに移動した場合しかし、私はそこで、基本的

404 Page Not Foundを取得し、私は、私はここで何をしないのです26.

のうち20件のコメントを見ることができますか?

EDIT:これは私はいくつかの調整後にスムーズに働いていたこの溶液に来た研究の時間後paginate_links()

<div class=""> 
    <span class="page-numbers current">1</span> 
    <a class="page-numbers" href="http://localhost/sciencr/profile/jane-flegal/page/2/">2</a> 
    <a class="page-numbers" href="http://localhost/sciencr/profile/jane-flegal/page/3/">3</a> 
    <a class="next page-numbers" href="http://localhost/sciencr/profile/jane-flegal/page/2/">Next »</a> 
</div> 
+0

あなたは私paginate_links()関数をエコーHTMLコードを与えることができますか? –

+0

@DmitriyButeiko元のコードを更新し、HTML出力を追加しました。 – Halnex

+0

ソリューションをご覧くださいhttps://wordpress.stackexchange.com/questions/176347/pagination-returns-404-after-page-20 –

答えて

関連する問題