如何创建分页

时间:2013-03-12 23:49:51

标签: wordpress pagination

有人可以向我解释如何为WordPress数据库中的内容实现分页吗?我们有大约2000条记录,我不想在一个页面中显示它们。

有人可以帮忙吗? 这是我通过这个代码我可以显示我的数据库的内容

<?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 个答案:

答案 0 :(得分:0)

使用编辑帖子/页面中的<!--nextpage-->选项。但是您需要使用一个知道如何翻译<!--nextpage-->标签并为页面编号并添加指向上一页和下一页的链接的主题。

许多自定义主题不会自动支持此功能。您可能必须添加它,或者选择一个支持它的主题。

答案 1 :(得分:0)

你需要的不仅仅是尺寸::

$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;
相关问题