如果帖子是通过get_posts()动态获取的,如何为帖子页面添加分页;

时间:2019-02-12 07:09:03

标签: php wordpress pagination get

我正在WordPress网站上工作,在该网站上有一个页面,其中包含按选择选项过滤的帖子。选择具有不同资源类型的选项。对于每种资源类型,我还有100多个帖子,但是我希望每页显示10个帖子,其余的应该进行分页。 我的代码:        

           $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                $args = array(
                'posts_per_page'   =>10,
                'offset'           => 0,
                'paged' => $paged,
                'orderby'          => 'post_date',
                'order'            => 'DESC',
                'post_type'        => 'post',
                'post_mime_type'   => '',
                'post_parent'      => '',
                'post_status'      => 'publish',
                'suppress_filters' => true 
             ); 
         if(isset($args3) && !empty($cat_termId)){ 

   $query = new WP_Query( array( 'category__and' => array($cat_termId) ) );

            $posts = $query->posts; 
         }else{
                $args1 = !empty($args2)?$args2:$args1; 
                $args =  array_merge($args,$args1);
                $posts =   get_posts( $args );                 

         }
         endif;

         if(!empty($posts) && !empty($cat_termId)){

            echo '<div class="row mt-5 insight-page-data search-insights" id="sub-insights">';
            foreach ($posts as $post){
         //Post Details
             $post_content = $post->post_content ;
             $post_excerpt = wp_trim_words($post->post_excerpt, 10) ;
             $post_name = $post->post_title ;
             $name = $post->post_name ;
             $link = get_the_permalink($post->ID);
             $image =wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID),'full'); 

             ?>

               <div class="col-sm-6 col-md-6 col-lg-4 my-3 wow fadeIn animated" data-wow-duration="1s" data-wow-delay=".3s">
                   <div class="card border-0 rounded-0 <?=$card?>" >
        <div class="card-header border-0 py-2">
                        <img src="<?=$card_link?>" alt="" class="mr-2"><?= ucfirst($main_slug_name)?>
                    </div>

                        <div class="card-img-top card-img  rounded-0 align-middle">
                        <a href="<?=$link?>"><img class="mx-auto d-block align-middle" src="<?php echo !empty($image[0])?$image[0]:$defaultImg?>" alt="MSR Cosmos <?= $main_slug_name?>"/></a>
                        </div>
                    <div class="card-body" id="alink1">
                      <h4 class="card-title"><a href="<?=$link?>" rel="noreferrer"><?=$name?></a></h4>
                      <p class="card-text"><?=$post_excerpt?></p>
                    </div>
                  </div>
    </div>

         <?php     } 
             echo '</div>';
            } else{

      echo "<p class='no-results'>No Insights Available</p>";
          }
         } 

          ?>    

请帮助我获得理想的结果。我真的在正确的道路上吗?

1 个答案:

答案 0 :(得分:0)

使用此代码在网站上添加分页。

$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$argument = array(
  'post_type' => 'post',
  'posts_per_page' => 10,
  'orderby'=>'date',
  'order'=>'DESC',
  'paged' => $paged
);    
$loop = new WP_Query($argument); ?>
<ul class="products columns-4">
<?php
 while ($loop->have_posts()) : $loop->the_post();
 endwhile;  ?>
 </ul>
 <div class="pagination" style="font-size: 25px !important;">
  <?php 
    echo paginate_links( array(
        'base'         => str_replace( 999999999, '%#%', esc_url(get_pagenum_link( 999999999 ) ) ),
        'total'        => $loop->max_num_pages,
        'current'      => max(
        1, get_query_var( 'paged' ) ),
        'format'       => '?paged=%#%',
        'show_all'     => false,
        'type'         => 'plain',
        'end_size'     => 2,
        'mid_size'     => 1,
        /*'prev_next'    => true,
        'prev_text'    => sprintf( '<i></i> %1$s', ( 'Newer Posts', 'text-domain' ) ),
        'next_text'    => sprintf( '%1$s <i></i>', ( 'Older Posts', 'text-domain' ) ),*/
        'add_args'     => false,
        'add_fragment' => '',
    ) );
?>
</div>