Как сделать разбиение на страницы внутри foreach? - PullRequest
0 голосов
/ 21 июня 2020

У меня есть код, который принимает комментарии (wordpress) из большой категории, но база данных большая. Я ограничиваю его 20 сообщениями ($ ppp), но я хочу сделать разбивку на страницы, чтобы этот код отображал 20 сообщений на каждой следующей странице. Вот код:

<?php // Posts per page setting
$ppp = 20;
$custom_offset = 0; // If you are dealing with your custom pagination, then you can calculate the value of this offset using a formula

// category (can be a parent category)
$category_parent = 36241;

// lets fetch sub categories of this category and build an array
$categories = get_terms( 'category', array( 'child_of' => $category_parent, 'hide_empty' => false ) );
$category_list =  array( $category_parent );
foreach( $categories as $term ) {
    $category_list[] = (int) $term->term_id;
}

// fetch posts in all those categories
$posts = get_objects_in_term( $category_list, 'category' );

$sql = "SELECT comment_ID, comment_date, comment_content, comment_post_ID
FROM {$wpdb->comments} WHERE
comment_post_ID in (".implode(',', $posts).") AND comment_approved = 1
ORDER by comment_date DESC LIMIT $ppp OFFSET $custom_offset";

$comments_list = $wpdb->get_results( $sql );

if ( count( $comments_list ) > 0 ) {
    $date_format = get_option( 'date_format' );
    echo '<ul>';
    foreach ( $comments_list as $comment ) {
        $comid = $comment->comment_ID;
        $datum = get_the_date();
        echo '<li><span> '.substr( $comment->comment_content, 0, 1000 ).'..</span>'.'<div class="small-t2"><i class="fa fa-book"></i><a href="'.get_permalink( $comment->comment_post_ID ).'#comment-'. $comid .'" rel="external nofollow">'.get_the_title( $comment->comment_post_ID ).'</a></div><div class="small-t1"><i class="fa fa-calendar"></i>'.$datum.'</div></li>';
    }
    echo '</ul>';

} else {
    echo '<p>No comments</p>';
}
?>
...