How to create pagination [closed]

2019-09-30 03:12发布

Could anyone explain to me how to implement pagination for the content we have in a WordPress database? We have around 2000 records and I don't want to display them in a single page.

Could anybody help? This is the code i have through this i can display the contents of my database

<?php
require_once( dirname(__FILE__) . '/wp-load.php' );
global $wpdb;
$table = $wpdb->prefix."car_saver";  
$data = $wpdb->get_results("SELECT * FROM " . $table );
if(count($data))
{
?>
<table width="700" cellspacing="0" border="0">
  <thead>
     <tr>
        <th>id</th>
        <th>Make</th>
        <th>Model</th>
        <th>Sub Model</th>
        <th>MSRP</th>
        <th>Utility</th>  
        <th>Feature</th>            
        <th>Year</th>   
      </tr>
  </thead>
  <tfoot>
        <tr>
        <th>id</th>
        <th>Make</th>
        <th>Model</th>
        <th>Sub Model</th>
        <th>MSRP</th>
        <th>Utility</th>  
        <th>Feature</th>            
        <th>Year</th>   
      </tr>
  </tfoot>
  <tbody>
  <?php 
    foreach($data as $p): ?>
     <tr>
      <td align="center"><?php echo $p->car_id; ?></td>
      <td align="center"><?php echo $p->Make; ?></td>
      <td align="center"><?php echo $p->Model; ?></td>
      <td align="center"><?php echo $p->subModel; ?></td>
      <td align="center"><?php echo $p->MSRP; ?></td>
      <td align="center"><?php echo $p->Utility; ?></td>
      <td align="center"><?php echo $p->Feature; ?></td>
      <td align="center"><?php echo $p->Year; ?></td>
     </tr>
     <?php endforeach; ?>
  </tbody>
  </table>
<?php 
}
else echo "data not found";
?> 

2条回答
\"骚年 ilove
2楼-- · 2019-09-30 03:35

Use the <!--nextpage--> option in Edit Post/Page. But you'll need to be using a theme that knows how to translate the <!--nextpage--> tag and number the pages and add links to prev and next pages.

Many custom themes don't support this automatically. You may have to add it, or pick a theme that supports it already.

查看更多
你好瞎i
3楼-- · 2019-09-30 03:41

You need more than try this for size::

$results_per_page = 10;
$current = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;

global $wpdb;

$table = $wpdb->prefix."car_saver";  
$rows = $wpdb->get_results("SELECT * FROM " . $table );


if( !empty($wp_query->query_vars['s']) ):
 $args['add_args'] = array('s'=>get_query_var('s'));
endif;

$args = array(
  'base' => @add_query_arg('paged','%#%'),
  'format' => '',
  'total' => ceil(sizeof($rows)/$results_per_page),
  'current' => $current,
  'show_all' => false,
  'type' => 'plain',
  );

  echo paginate_links($args);
  $start = ($current - 1) * $results_per_page;
  $end = $start + $results_per_page;
  $end = (sizeof($rows) < $end) ? sizeof($rows) : $end;
  echo '<br />';

 for($i=$start;$i < $end ;++$i ):
  $row = $rows[$i];
 endfor;
查看更多
登录 后发表回答