admin管理员组

文章数量:1127175

Pagination works in the front page but in custom post type page it doesn't appear.

Post type:

add_action('init', 'portfolio_register');    

function portfolio_register() {
    $args = array(
        'label' => __('Portfolio'),
        'singular_label' => __('Project'),
        'public' => true,
        'show_ui' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'has_archive' => true,
        'rewrite' => true,
        'supports' => array('title', 'editor', 'thumbnail', 'custom-fields')
       );    

    register_post_type( 'portfolio' , $args );
    flush_rewrite_rules();
}

Pagination function by Kriesi:

function kriesi_pagination($pages = '', $range = 2)
{  
     $showitems = ($range * 2)+1;  

     global $paged;
     if(empty($paged)) $paged = 1;

     if($pages == '')
     {
         global $wp_query;
         $pages = $wp_query->max_num_pages;
         if(!$pages)
         {
             $pages = 1;
         }
     }   

     if(1 != $pages)
     {
         echo "<div class='blogpagination'>";
         if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>&laquo;</a>";
         if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo;</a>";

         for ($i=1; $i <= $pages; $i++)
         {
             if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
             {
                 echo ($paged == $i)? "<span class='blogcurrent'>".$i."</span>":"<a href='".get_pagenum_link($i)."' class='bloginactive' style='color:rgb(70, 70, 70);'>".$i."</a>";
             }
         }

         if ($paged < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($paged + 1)."'>&rsaquo;</a>";  
         if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>&raquo;</a>";
         echo "</div>\n";
     }
}

<?php kriesi_pagination(); ?> To display pagenation.


Update

The loop:

  <div class="container container-2 clearfix">
          <?php $loop = new WP_Query( array( 'post_type' => 'portfolio', 'posts_per_page' => -1 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    <div class="container container-3 clearfix">
        <a href="<?php the_permalink() ?>">
                    <div class="element _element"></div>

<?php echo get_the_post_thumbnail( $post_id, 'full', array( 'class' => 'image image-2' ) ); ?>
        </a>
    </div>
      <?php endwhile; wp_reset_query(); ?>
          <div class="pagenat" > <?php kriesi_pagination(); ?> </div>
  </div>

Pagination works in the front page but in custom post type page it doesn't appear.

Post type:

add_action('init', 'portfolio_register');    

function portfolio_register() {
    $args = array(
        'label' => __('Portfolio'),
        'singular_label' => __('Project'),
        'public' => true,
        'show_ui' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'has_archive' => true,
        'rewrite' => true,
        'supports' => array('title', 'editor', 'thumbnail', 'custom-fields')
       );    

    register_post_type( 'portfolio' , $args );
    flush_rewrite_rules();
}

Pagination function by Kriesi:

function kriesi_pagination($pages = '', $range = 2)
{  
     $showitems = ($range * 2)+1;  

     global $paged;
     if(empty($paged)) $paged = 1;

     if($pages == '')
     {
         global $wp_query;
         $pages = $wp_query->max_num_pages;
         if(!$pages)
         {
             $pages = 1;
         }
     }   

     if(1 != $pages)
     {
         echo "<div class='blogpagination'>";
         if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>&laquo;</a>";
         if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo;</a>";

         for ($i=1; $i <= $pages; $i++)
         {
             if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
             {
                 echo ($paged == $i)? "<span class='blogcurrent'>".$i."</span>":"<a href='".get_pagenum_link($i)."' class='bloginactive' style='color:rgb(70, 70, 70);'>".$i."</a>";
             }
         }

         if ($paged < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($paged + 1)."'>&rsaquo;</a>";  
         if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>&raquo;</a>";
         echo "</div>\n";
     }
}

<?php kriesi_pagination(); ?> To display pagenation.


Update

The loop:

  <div class="container container-2 clearfix">
          <?php $loop = new WP_Query( array( 'post_type' => 'portfolio', 'posts_per_page' => -1 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    <div class="container container-3 clearfix">
        <a href="<?php the_permalink() ?>">
                    <div class="element _element"></div>

<?php echo get_the_post_thumbnail( $post_id, 'full', array( 'class' => 'image image-2' ) ); ?>
        </a>
    </div>
      <?php endwhile; wp_reset_query(); ?>
          <div class="pagenat" > <?php kriesi_pagination(); ?> </div>
  </div>
Share Improve this question edited Apr 12, 2015 at 15:52 Ali Almoullim asked Apr 12, 2015 at 14:17 Ali AlmoullimAli Almoullim 1116 bronze badges 4
  • 1 In which template, and please post your code for that specific template. Also, do not run flush_rewrite_rules(); on init and every page load. It is a really expensive function. You should run it once and delete it, or add it to the appropriate hook – Pieter Goosen Commented Apr 12, 2015 at 14:21
  • @PieterGoosen I'm using a template i made, and which part do you want me to post? the custom post type page? .... i don't know how this can help anyway. – Ali Almoullim Commented Apr 12, 2015 at 15:45
  • Without knowing your loop and template, it is impossible to say why pagination does not show. What I know is, the pagination code works perfectly – Pieter Goosen Commented Apr 12, 2015 at 15:49
  • @PieterGoosen Updated the post (included the lopp) – Ali Almoullim Commented Apr 12, 2015 at 15:52
Add a comment  | 

1 Answer 1

Reset to default 0

Change

$loop = new WP_Query( array( 'post_type' => 'portfolio', 'posts_per_page' => -1 ) );

to e.g.

$loop = new WP_Query( array( 'post_type' => 'portfolio', 'posts_per_page' => 9 ) );

If you set posts_per_page to -1 you will always pull all available posts. Hence, no pagination will show up since all posts are already shown on the first page.

本文标签: loopPagination on custom post types