当结果由外部资源提供时,在 Wordpress 中强制分页

Forcing Pagination in Wordpress, when results are provided by an external resource

我对 Wordpress 很头疼。我正在构建一个连接到 CRM 的 Wordpress 前端,我用它来提取库存详细信息,然后将其显示在 Wordpress 中。

一切正常,除了一件事 - 分页。我一直在通过分页 var 和使用各种插件进行 Wordpress 分页,但我的查询是手工编码的 PHP 不使用 Wordpress 功能的 PDO 请求。

有谁知道如何在 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> &pound;<?php echo $vehicle_deposit_min; ?> | 
                            <strong>From</strong> &pound;<?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' );
                    ?>


                    ....

我不太明白什么地方不起作用,但您准备声明的方式有误。而不是

$stmt = $dbh->prepare("SELECT * FROM cars LIMIT 0,$limit");

应该是

$stmt = $dbh->prepare("SELECT * FROM cars LIMIT 0, ?");

然后你可以限制为 1

$stmt->bindParam(1, $id, PDO::PARAM_INT);

编辑 - 如果您想进行分页,查询必须保持页面计数。

所以让我们从查询开始

$stmt = $dbh->prepare('SELECT * FROM cars LIMIT :lower, :upper ');

$per_page = 5;
$page = 1;
if ( isset( $_GET['page'] ) ) {
  $page = $_GET['page'];
}
$upper =  $per_page * $page;
$lower = $upper - $per_page;
$stmt->bindParam(':lower', $lower);
$stmt->bindParam(':upper ', $upper );
$stmt->execute();
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $data;

然后您必须使用 'page'

参数创建 Next/Prev 链接

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>";
}

首先,想一想,无论 worpdress 还是任何其他 cms,分页是如何工作的。页面列表属于编号,即 2,3,4...7,8。当有人点击任何页面 link 时,它会提供 $_GET 值。然后我们在查询限制中使用这个值。还有另一种方法是从数据库中检索所有数据,拆分为页面。但是,您应该尝试第一种方法。有很多分页 类 和教程,选择其中一个并检查其工作原理并根据需要实施。我想这个 link 会对你有帮助 link