如何将数字分页添加到此博客页面?

时间:2020-07-14 作者:social

我想将数字分页添加到我的博客页面。我试图添加分页代码,但没有出现。这是代码,我想给它添加数字分页。这是博客页面

<?php
               $args = array(
                    \'post_type\' => \'post\'
                );
            
                $post_query = new WP_Query($args);

            if($post_query->have_posts() ) {
                while($post_query->have_posts() ) {
                    $post_query->the_post();
                    ?>

         <div class="cbp-item">
            <div class="news_item shadow">
               <a class="image" href="<?php the_permalink();?>">
                  <img src="<?php the_post_thumbnail_url(); ?>" alt="Latest News" class="img-responsive">
               </a>
               <div class="news_desc">
                  <h3 class="text-capitalize font-light darkcolor"><a href="<?php the_permalink();?>"><?php the_title();?></a></h3>
                  <ul class="meta-tags top20 bottom20">
                     <li><a href="#."><i class="fa fa-calendar"></i><?php echo get_the_date( \'M d\' ); ?></a></li>
                     <li><a href="#."> <i class="fa fa-user" aria-hidden="true"></i> <?php the_author_posts_link();?> </a></li>
                     <li><a href="#."><i class="fa fa-comment-o"></i><?php comments_number( \'no responses\', \'one response\', \'% responses\' ); ?></a></li>
                  </ul>
                  <p class="bottom35"><?php the_excerpt(); ?></p>
                  <a href="<?php the_permalink();?>" class="button btnprimary btn-gradient-hvr">Read more</a>
               </div> 
            </div> 
         </div> 

         <?php
            }
        }
        
           ?>

1 个回复
SO网友:giannisrig

首先,需要获取分页查询变量并将其传递给主查询参数:

//Protect against arbitrary paged values
$paged = ( get_query_var( \'paged\' ) ) ? absint( get_query_var( \'paged\' ) ) : 1;

$args = array(
    \'post_type\' => \'post\'
    \'paged\' => $paged,
);
然后,必须在要显示分页的位置添加以下代码:

<?php
    $big = 999999999; // need an unlikely integer
     
    echo paginate_links( array(
        \'base\'      => str_replace( $big, \'%#%\', esc_url( get_pagenum_link( $big ) ) ),
        \'format\'    => \'?paged=%#%\',
        \'current\'   => max( 1, get_query_var(\'paged\') ),
        \'total\'     => $post_query->max_num_pages
    ) );
?>

相关推荐

bloginfo url in javascript

我正在试着设置<?php bloginfo(\'template_url\'); ?> 在Javascript上的变量中,然后设置src 的img 元素,但在html中它被打印为src=\"<?php bloginfo(\'template_url\'); ?>\" 事实上url.我就是这样做的:var wpTemplateUrl = \"<?php bloginfo(\'template_url\'); ?>\"; primaryImg.src = wpTem