【问题标题】:Custom post pagination自定义帖子分页
【发布时间】:2014-07-27 22:15:32
【问题描述】:

我正在尝试在自定义模板中对我的自定义帖子类型进行分页。我是这样尝试的:

<?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 文件,并且不显示来自自定义帖子类型的下一个项目。我哪里做错了?

【问题讨论】:

    标签: php wordpress pagination custom-post-type


    【解决方案1】:

    这个

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

    【讨论】:

      【解决方案2】:

      在你的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>';
      
      }
      

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

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

      我觉得对你有帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-01-13
        • 2014-12-11
        • 2014-05-15
        • 2011-03-20
        • 2014-05-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多