skip to Main Content

The below php code results pagination as 1, 2, 3, 4, 5, 6 , …

I would like to get the result as 1, 2, 3, … ,10

    if ( is_array( $pages ) ) {
        $paged = ( get_query_var( 'paged' ) == 0 ) ? 1 : get_query_var( 'paged' );
        echo '<ul class="page-numbers nav-pagination links text-center">';
        foreach ( $pages as $page ) {
            $page = str_replace( 'page-numbers', 'page-number', $page );
            echo '<li>' . $page . '</li>';
        }
        echo '</ul>';
    }
?>

2

Answers


  1. I want to share an example.

    Adding dots between numbers of pagination

    if ( is_array( $pages ) ) {
    
    $paged = ( get_query_var( 'paged' ) == 0 ) ? 1 : get_query_var( 'paged' );
    
    echo '<ul class="page-numbers nav-pagination links text-center">';
    
    $i = '0';
    
    foreach ( $pages as $page ) {
    
    $page = str_replace( 'page-numbers', 'page-number', $page );
    
        $i++;
    
        if($i == 4){
    
            echo '...';
    
        } elseif($i < 4 || $i > (count($pages) -3)){
    
            echo '<li>' . $page . '</li>';
    
        }
    
    }
    
    echo '</ul>';
    
    }
    

    I think it works fine.

    Correction: You can fix $i == ‘4’ and add "…" after the number you specified. Or you can find the median value of the $pages array and replace it with 4.

    Login or Signup to reply.
  2. Based on the code I suppose that the $pages is an array containing the page numbers 1-10 and you want to get the first n and the last number listed.

    There are several solutions for this depending on the use case.

    Solution 1

    If the listing always starts at 1 and you can hard-code the first three elements and the ellipsis and only add the last page number to the list:

    $pages = [1,2,3,4,5,6,7,8,9,10];
    $n = 3;
    
    for($k=1;$k<=$n;$k++) {
       echo "<li>$k</li>";
    }
    echo "<li>...</li><li>" . count($pages) . "</li>";
    

    Solution 2

    If for some reason you have to iterate through the whole $pages array, then you can add an ellipsis after the n-th element and don’t print for the rest but for the last one:

    $pages = [1,2,3,4,5,6,7,8,9,10];
    $n = 3;
    
    foreach($pages as $page) {
        if($page <= $n || $page == count($pages))
            echo "<li>$page</li>";
        else if($page == $n+1) 
            echo "<li>...</li>";
        
        // something else for all pages
    }
    

    Solution 3

    You can also use some advanced indexing to get the first n elements plus the last one and run the loop only for those (this also works if the page numbers are not 1,2,3,…)

    $pages = ['I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X'];
    $n = 3;
    
    foreach(array_merge(range(0, $n-1), [count($pages)-1]) as $idx) {
        echo "<li>$pages[$idx]</li>";
        
        if($idx == $n-1) {
            echo "<li>...</li>";
        }
       
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search