我有一个Wordpress头痛。我正在建立一个Wordpress前端,该前端连接到我用来提取库存详细信息的CRM上,然后将其显示在Wordpress中。
除了一件事-分页,一切都正常。我一直通过分页的var并使用各种插件来进行Wordpress分页,但是我的查询是手工编码的PHP PDO请求,没有利用Wordpress的功能。
即使结果是由外部资源提供的,没有人知道如何在Wordpress中强制分页吗?
如果有帮助,我的查询如下(在functions.php中):
$dbh = new PDO("mysql:host=$hostname;dbname=cl15-l3ase2015", $username, $password);
$stmt = $dbh->prepare("SELECT * FROM cars LIMIT 0,$limit");
$stmt->bindParam(1, $id, PDO::PARAM_INT);
$stmt->execute();
$total = $stmt->rowCount();
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
$data['total_results'] = $total;
return $data;
...并且模板受以下内容控制:
<div class="col-xs-12 col-sm-8 col-md-8 col-lg-8 mask-left mask-right">
<div class="body-copy-alt">
<?php
$posts_per_page = 2;
$post_count = $total;
$vehicle_group = get_latest_vehicles(10000);
$total = $vehicle_group['total_results'];
?>
<div class="col-xs-12 col-sm-7 col-md-8 col-lg-8 no-padding-left">
<h4 class="top"><strong><span id="total-holder"><?php echo $total;?></span> vehicles found</strong>, drive away in five days</h4>
</div>
<div class="col-xs-12 col-sm-5 col-md-4 col-lg-4 no-padding-right mask-left">
<select name="refine" id="refine">
<option value="null" disabled>Select Lowest Deposit</option>
<option value="100">100</option>
</select>
</div>
<div class="clearfix"></div>
<?php
foreach($vehicle_group as $current_vehicle) {
$current_id = $current_vehicle['id'];
$vehicle_manufacturer = $current_vehicle['carMake'];
$vehicle_model = $current_vehicle['carModel'];
$vehicle_registration = $current_vehicle['carReg'];
$vehicle_fuel = get_specific('FUEL', $current_vehicle['fuel']);
$vehicle_transmission = get_specific('TRANSMISSION', $current_vehicle['transmission']);
$vehicle_mileage = $current_vehicle['mileage'];
$vehicle_mpg = $current_vehicle['economy'];
$vehicle_year = $current_vehicle['year'];
$finance_data = get_finance_data($current_id);
$vehicle_deposit_min = $finance_data['minDeposit'];
$vehicle_weekly_payments = $finance_data['weeklyPayments'];
$gallery_data = get_gallery_images($current_id);
if(isset($current_id)) {
?>
<!-- Vehicle result sample -->
<div class="search-result">
<img src="<?php bloginfo('template_directory');?>/assets/img/new-stock-ribbon.png" class="ribbon" />
<div class="col-xs-12 col-sm-4 col-md-4 col-lg-6 no-padding-left no-padding-right">
<form id="vehicle-<?php echo $current_id;?>" method="post" action="<?php bloginfo('url');?>/vehicle">
<input type="hidden" name="vehicle" value="<?php echo $current_id; ?>" />
<a href="#" onclick="$('#vehicle-<?php echo $current_id; ?>').submit();">
<img src="http://79.170.40.161/lease2buy.co.uk/uploads/<?php echo $current_id;?>/<?php echo $gallery_data[0]['photo']; ?>" class="vehicle-thumb" />
</a>
</form>
</div>
<div class="col-xs-12 col-sm-8 col-md-8 col-lg-6">
<h4><?php echo $vehicle_manufacturer; ?></h4>
<i><?php echo $vehicle_model; ?></i>
<div class="clearfix"></div>
<table class="table-responsive" summary="Vehicle specifications">
<tbody>
<tr>
<td class="legend">Reg No</td>
<td><?php echo strtoupper($vehicle_registration); ?></td>
<td class="spacer"></td>
<td class="legend">Year</td>
<td><?php echo $vehicle_year; ?></td>
</tr>
<tr>
<td class="legend">Mileage</td>
<td><?php echo $vehicle_mileage; ?></td>
<td class="spacer"></td>
<td class="legend">Fuel Type</td>
<td><?php echo $vehicle_fuel; ?></td>
</tr>
<tr>
<td class="legend">Transmission</td>
<td><?php echo $vehicle_transmission; ?></td>
<td class="spacer"></td>
<td class="legend">MPG</td>
<td><?php echo $vehicle_mpg; ?></td>
</tr>
</tbody>
</table>
<div class="col-xs-6 col-sm-6 col-md-5 col-lg-5 no-padding-left">
<form name="vehicle_interest" method="post" action="<?php bloginfo('url');?>/vehicle/">
<input type="hidden" name="vehicle" value="<?php echo $current_id; ?>" />
<input type="submit" class="button bg-lightblue" value="View Details" />
</form>
</div>
<div class="col-xs-6 col-sm-6 col-md-5 col-lg-5 no-padding-right">
<form name="vehicle-<?php echo $current_id; ?>" method="post" action="<?php bloginfo('url');?>/vehicle">
<input type="hidden" name="vehicle" value="<?php echo $current_id; ?>" />
<input type="submit" class="button bg-green" value="More Info" />
</form>
</div>
<div class="clearfix"></div>
</div>
<div class="clearfix"></div>
</div> <!-- /.search-result -->
<div class="clearfix"></div>
<div class="result-bottom">
<div class="col-xs-12 col-sm-4 col-md-4 col-lg-4 gallery-link">
<a href="#"><img src="<?php bloginfo('template_directory');?>/assets/img/icon-camera.png" /> <?php echo $gallery_data['total_results']; ?></a>
</div>
<div class="hidden-xs col-sm-8 col-md-8 col-lg-8">
<h5 class="text-white"><strong>Min Deposit</strong> £<?php echo $vehicle_deposit_min; ?> |
<strong>From</strong> £<?php echo $vehicle_weekly_payments; ?> per week</h5>
</div>
</div>
<!-- END Search result sample -->
<?php } } ?>
<?php
global $wp_query;
$wp_query->max_num_pages = ceil( $post_count / $posts_per_page );
// pagination functions
next_posts_link( 'Older Entries' );
previous_posts_link( 'Newer Entries' );
?>
....
WordPress next_posts_link和previous_posts_link所做的所有操作均以正确的格式创建链接,以获取下一页和上一页。由于您使用的不是帖子或页面,因此这些链接的格式不正确。
如果为数据库起始位置引入其他参数,则可以使用自定义CRM来完成相同的操作。详细信息将取决于您的应用程序的确切工作方式,但基础知识是:
您可以修改代码,以引入一些了解数据库中从何处开始的方法。例如:
$dbh = new PDO("mysql:host=$hostname;dbname=cl15-l3ase2015", $username, $password);
$start = isset($_GET['start']) ? $_GET['start'] : 0;
$stmt = $dbh->prepare("SELECT * FROM cars LIMIT $start,$limit");
$stmt->bindParam(1, $id, PDO::PARAM_INT);
$stmt->execute();
$total = $stmt->rowCount();
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
$data['total_results'] = $total;
return $data;
您可以创建自己的功能来完成相同的事情。
例如,如果$ start是当前页面的起始位置:
function my_next_link($start, $limit) {
$new_start = $start + $limit;
if ($new_start <= number of records in the CRM) {
echo "<a href=\"http://example.com/mypage?start=$start\">Next</a>";
}
}
function my_previous_link($start, $limit) {
if ($start > 0) {
$new_start = max(0, $start - $limit);
echo "<a href=\"http://example.com/mypage?start=$start\">Previous</a>";
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句