对自定义帖子类型进行分页

时间:2015-04-12 作者:Ali Almoullim

分页在首页有效,但在自定义帖子类型页面中不会显示。enter image description here

岗位类型:

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();
}
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(); ?> 显示寻呼。

更新循环:

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

1 个回复
SO网友:wbq

改变

$loop = new WP_Query( array( \'post_type\' => \'portfolio\', \'posts_per_page\' => -1 ) );
至,例如。

$loop = new WP_Query( array( \'post_type\' => \'portfolio\', \'posts_per_page\' => 9 ) );
如果您设置posts_per_page-1 您将始终调出所有可用的帖子。因此,不会显示分页,因为所有帖子都已显示在第一页上。

结束

相关推荐

Loop for sticky posts

我用过Justin Tadlock\'s 关于如何创建仅包含粘性帖子的循环的教程。代码大致如下所示:$sticky = get_option( \'sticky_posts\' ); rsort( $sticky ); $sticky = array_slice( $sticky, 0, 2 ); query_posts( array( \'post__in\' => $sticky, \'caller_get_posts\' => 1 ) ); 根据教程,我