Я сделал один шаблон и назначил шаблон для страницы отзывов. Весь приведенный ниже код помещен в мой файл шаблона.
Моя проблема в том, что ссылки Next и Prev даже не отображаются на странице отзывов. Я хочу использовать только запрос get_posts для извлечения данных.
Как я могу отобразить нумерацию страниц для get_posts?
global $post;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 0;
$postsPerPage = 2;
$postOffset = $paged * $postsPerPage;
$args_testimonial = array(
'post_type' => 'testimonial',
'offset' => $postOffset,
'posts_per_page' => $postsPerPage,
'orderby'=> 'title',
'order'=>'DESC'
);
$data_testimonial = get_posts( $args_testimonial );
foreach ( $data_testimonial as $post_testimonial ) : setup_postdata( $post_testimonial );
$testimonee_title = get_the_title($post_testimonial->ID);
$testimonee_post = get_post($post_testimonial->ID);
$testimonee_content = $testimonee_post->post_content;
$testimonee_image = wp_get_attachment_image_src( get_post_thumbnail_id( $post_testimonial->ID ), 'single-post-thumbnail' );
if ( $testimonee_title || $testimonee_content || $testimonee_image ) { ?>
<div class = "single-testimonial">
<?php if ( $testimonee_content ) { ?>
<div class = "testimonial-content">
<p>Dear Gary’s Carpet Cleaning</p>
<p><?php echo $testimonee_content; ?></p>
</div>
<?php }
if ( $testimonee_title || $testimonee_image ) { ?>
<div class = "testimonial-person">
<div class = "testimonial-image">
<?php if ( $testimonee_image ) { ?>
<img src = "<?php echo $testimonee_image[0]; ?>" alt = " "/>
<?php } else { ?>
<img src = "<?php echo get_template_directory_uri(); ?>/assets/images/testimonial-dummy-image.png" alt = " "/>
<?php } ?>
</div>
<?php if ( $testimonee_title ) { ?>
<div class = "testimonial-name">
<p><strong>-<?php echo $testimonee_title; ?></strong></p>
</div>
<?php } ?>
</div>
<?php } ?>
</div>
<?php } endforeach;
next_posts_link( 'Prev');
previous_posts_link('Next');
wp_reset_postdata();
@rowmoin Еще нет, я отправил этот вопрос для решения
Я попытался дать решение в разделе ответов ниже.






Вы можете попробовать заменить код разбивки на страницы, но я не уверен, что он будет работать или нет:
<?php
{$paged=$_GET['current-page'];}else{$paged=1;}
$total_pages=$data_testimonial ->max_num_pages;
if ($total_pages>1){$current_page=$paged;
?>
<?php
echo '<ul class = "pagination"><li >';
$big=999999999;
echo paginate_links (array('base'=>@add_query_arg('current-page','%#%'),
'format'=>'?current-page=%#%','current'=>$current_page,'total'=>$total_pages,'mid_size'=>4,'type'=>'plain',
'prev_next'=>True,));
echo '</li>
</ul>';?>
<?php }wp_reset_postdata();wp_reset_query();?>
Пожалуйста, не используйте get_posts, если вам нужны запросы с разбивкой на страницы.
Я думаю, что самым простым и подходящим здесь является использование WP_Query для создания собственного запроса.
Я считаю, что next_posts_link() и previous_posts_link() лучше использовать с WP_Query
global $post;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 0;
$postsPerPage = 2;
$postOffset = $paged * $postsPerPage;
$args_testimonial = array(
'post_type' => 'testimonial',
'offset' => $postOffset,
'posts_per_page' => $postsPerPage,
'orderby'=> 'title',
'order'=>'DESC'
);
$postslist = new WP_Query( $args_testimonial );
if ( $postslist->have_posts() ) :
while ( $postslist->have_posts() ) : $postslist->the_post();
the_title();
endwhile;
next_posts_link( 'Older Entries', $postslist->max_num_pages );
previous_posts_link( 'Next Entries »' );
wp_reset_postdata();
endif;
Я проверил везде, наконец, спустя долгое время я решил это.
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$postsPerPage=9;
$postOffset = $paged * $postsPerPage;
$past_events = get_posts(
array(
'product_cat' => 'past-events',
'numberposts' => $postsPerPage,
'post_type' => 'product',
'order' => 'DESC',
'offset' => $postOffset-$postsPerPage,
)
);
foreach($past_events as $key => $val){
print_r($val);
}
Pratab Есть ли способ получить общее количество сообщений одним и тем же методом get_posts? (без использования дополнительного запроса.)
ты нашел какое-нибудь решение?