Wordpress分页不适用于" get_posts"

时间:2018-04-25 10:50:46

标签: php wordpress pagination

我制作了一个模板并将模板分配给推荐页面。 以下所有代码都放在我的模板文件中。

我的问题是NextPrev链接甚至没有显示在推荐页面上。我只想使用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();

3 个答案:

答案 0 :(得分:0)

您可以尝试更换您的分页代码,但不确定它是否有效:

 <?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();?>

答案 1 :(得分:0)

如果您需要分页查询,请不要使用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 &raquo;' ); 
        wp_reset_postdata();
        endif;

答案 2 :(得分:0)

长时间解决后,我终于检查了所有地方。

FROM golang:latest as builder
COPY  etc/ssl/ /etc/ssl/certs/ #❗️add certificate to container 
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download