如何在wordpress中添加自定义分页

时间:2015-07-04 06:31:57

标签: wordpress post pagination

我在wordpress中创建了自定义主题。 我想在自定义帖子模板中添加自定义分页,即INDEX.PHP

请您检查分页脚本中的错误。实际上我已经设置了4个帖子限制,并且在我的博客中有大约8个帖子..当点击2分页时,它不会移动到下一页...

shouldComponentUpdate

请帮忙:) 谢谢, Harshad Patil

2 个答案:

答案 0 :(得分:2)

试试这个

$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;

// WP_Query arguments
$args = array (
    'post_type'              => 'YOUR_CUSTOM_POST_TYPE',
    'posts_per_page'         => '3',
    'paged' => $paged
);

// The Query
$cquery = new WP_Query( $args );
while ( $cquery->have_posts() ) : $cquery->the_post();
echo $post->ID;
endwhile;

$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' =>  $cquery->max_num_pages
) );

答案 1 :(得分:1)

在" functions.php"中添加以下代码文件:

function custom_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='pagination'>";
     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='current'>".$i."</span>":"<a href='".get_pagenum_link($i)."' class='inactive' >".$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";
 }
 }

在你的&#34; index.php&#34; file add&#34; custom_pagination();&#34;在while循环之后。