【问题标题】:How to add pagination for posts page if posts are getting dynamically by get_posts();如果帖子是通过 get_posts() 动态获取的,如何为帖子页面添加分页;
【发布时间】:2019-07-05 18:34:39
【问题描述】:

我正在开发一个 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>";
          }
         } 

          ?>    

请帮助我得到想要的结果。我真的走在正确的道路上吗?

【问题讨论】:

标签: php wordpress pagination get


【解决方案1】:

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

$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>

【讨论】:

  • 我已经尝试过使用 WP_Query,但没有按照我的要求得到想要的结果。是否可以使用 get_post 进行分页
  • 任何人帮我为该代码添加分页。还有其他方法吗?
  • 在使用上述代码获取分页和工作页面后,但我的页面模板有问题。实际上,我的页面中有过滤器概念。从其他模板获取的过滤数据。在第 2 页中,我得到了继续发布但获取包含该过滤器概念的模板。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-15
  • 1970-01-01
  • 1970-01-01
  • 2012-09-17
  • 2012-05-17
  • 2011-03-14
相关资源
最近更新 更多