繁体   English   中英

自定义分页

[英]Custom post pagination

我正在尝试对自定义模板中的自定义帖子类型进行分页。 我以这种方式尝试过:

<?php
      // The Query
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
      $the_query = new WP_Query( array( 'post_type' => 'portfolio', 'portfolio_category' => 'projektowanie', 'orderby' => 'date', 'order' => 'DESC', 'posts_per_page'=>3, 'paged'=>$paged ) );
      $i = 1;
      // The Loop
      while ( $the_query->have_posts() ) : $the_query->the_post();                                        
          $thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id ( $the_query->ID ), 'thumbnail' );
      ?> 

          <a href="#" data-toggle="modal" data-target="#pro<?php echo $i;?>" class="col-md-4 item-portfolio">
            <img src="<?php echo $thumbnail[0]; ?>" alt="">
            <h4><?php the_title(); ?></h4>
            <p><?php echo get_post_meta( $post->ID, 'subtitle', true ); ?></p>
          </a>
        <?php if($i % 3 == 0){ ?>
        <div class="clearfix"></div>
        <div style="margin-top: 0px; "class="shadow"></div> 

      <?php } $i++; endwhile; ?> 
      <nav>
<ul>
    <li><?php previous_posts_link( '&laquo; PREV', $the_query->max_num_pages) ?></li> 
    <li><?php next_posts_link( 'NEXT &raquo;', $the_query->max_num_pages) ?></li>
</ul>

当我单击“下一步”时,它将转到/ portfolio / page / 2 /,但是它将加载index.php而不是Portfolio.php文件,并且不会显示自定义帖子类型的下一项。 我在哪里弄错了?

对此感兴趣

<ul>
    <li><?php previous_posts_link( '&laquo; PREV', $the_query->max_num_pages) ?></li> 
    <li><?php next_posts_link( 'NEXT &raquo;', $the_query->max_num_pages) ?></li>
</ul>

用这个

<div class="pagination">
<?php             
    global $wp_query;

    $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' => $wp_query->max_num_pages
    ) );
?>
</div>

嘿,将此功能添加到您的function.php文件中

function custom_pagination($numpages = '', $pagerange = '', $paged='') {

  if (empty($pagerange)) {
    $pagerange = 2;
}

  global $paged;
  if (empty($paged)) {
    $paged = 1;
    }
  if ($numpages == '') {
    global $wp_query;
    $numpages = $wp_query->max_num_pages;
    if(!$numpages) {
        $numpages = 1;
    }
}
  /** 
   * We construct the pagination arguments to enter into our paginate_links
   * function. 
   */
  $pagination_args = array(
    'base'               => get_pagenum_link(1) . '%_%',
    'total'              => $numpages,
    'current'            => $paged,
    'show_all'           => False,
    'end_size'           => 1,
    'mid_size'           => $pagerange,
    'prev_next'          => True,
    'prev_text'          => __('&laquo;' , 'swift'),
    'next_text'          => __('&raquo;' , 'swift'),
    'type'               => 'array',
    'add_args'           => false,
    'add_fragment'      => ''
  );

  $paginate_links = paginate_links($pagination_args);
  echo '<div class="Pagination-Num"><ul>';
        foreach ( $paginate_links as $paginate_link ) {
                 echo "<li> $paginate_link </li>";
            }
        echo '</ul></div>';

}

然后在您的自定义模板文件中添加以下代码段

<?php
      // The Query
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
      $the_query = new WP_Query( array( 'post_type' => 'portfolio', 'portfolio_category' => 'projektowanie', 'orderby' => 'date', 'order' => 'DESC', 'posts_per_page'=>3, 'paged'=>$paged ) );
      $i = 1;
      // The Loop
      while ( $the_query->have_posts() ) : $the_query->the_post();                                        
          $thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id ( $the_query->ID ), 'thumbnail' );
      ?> 

          <a href="#" data-toggle="modal" data-target="#pro<?php echo $i;?>" class="col-md-4 item-portfolio">
            <img src="<?php echo $thumbnail[0]; ?>" alt="">
            <h4><?php the_title(); ?></h4>
            <p><?php echo get_post_meta( $post->ID, 'subtitle', true ); ?></p>
          </a>
        <?php if($i % 3 == 0){ ?>
        <div class="clearfix"></div>
        <div style="margin-top: 0px; "class="shadow"></div> 

      <?php } $i++; endwhile; ?> 
      <nav>
   <?php custom_pagination($the_query->max_num_pages,"",$paged);?>

   <?php
      if ($the_query->max_num_pages > 1) {
      ?>
    <ul>
      <li><?php echo get_next_posts_link( 'NEXT &raquo;', $the_query->max_num_pages );?> </li>
      <li><?php echo get_previous_posts_link( '&laquo; PREV' ); ?></li>
</ul>

我认为它可以帮助您

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM