2016年,我为一个客户端创建了简单的自定义WordPress插件,它只显示从自定义表查询的结果的分页列表。
try {
$recordcountquery = \'SELECT
FName,LName
FROM
tblBurialRegister WHERE\';
if (!empty($_SESSION[\'firstname\']))
{
$cond = $mydb->prepare(\' FName = %s\', $_SESSION[\'firstname\']);
$recordcountquery .= $cond;
}
if (!empty($_SESSION[\'lastname\']) && !empty($_SESSION[\'firstname\']))
{
$recordcountquery .= \' and\';
}
if (!empty($_SESSION[\'lastname\']))
{
$cond = $mydb->prepare(\' LName = %s\', $_SESSION[\'lastname\']);
$recordcountquery .= $cond;
}
$countresults = $mydb->get_results($recordcountquery);
$total = count($countresults);
// How many items to list per page
$limit = 20;
// How many pages will there be
$pages = ceil($total / $limit);
// What page are we currently on?
$page = get_query_var(\'page\');
if($page == 0)
{
$page = 1;
}
// Calculate the offset for the query
$offset = ($page - 1) * $limit;
// Some information to display to the user
$start = $offset + 1;
$end = min(($offset + $limit), $total);
// The "back" link
$prevlink = ($page > 1) ? \'<a href="?page=1" title="First page">«</a> <a href="?page=\' . ($page - 1) . \'" title="Previous page">‹</a>\' : \'<span class="disabled">«</span> <span class="disabled">‹</span>\';
// The "forward" link
$nextlink = ($page < $pages) ? \'<a href="?page=\' . ($page + 1) . \'" title="Next page">›</a> <a href="?page=\' . $pages . \'" title="Last page">»</a>\' : \'<span class="disabled">›</span> <span class="disabled">»</span>\';
if (count($countresults) > 0)
{
echo "<span id=\'results\' class=\'top-offset\'></span><p style=\'font-size:x-large;\'>Showing results for: ".$_SESSION[\'firstname\']." ".$_SESSION[\'lastname\']."</p><br/>";
// Display the paging information
echo \'<div id="paging" class="paging-text" style="font-size: large;"><p>\', $prevlink, \' Page \', $page, \' of \', $pages, \' pages, displaying \', $start, \'-\', $end, \' of \', $total, \' results \', $nextlink, \' </p></div>\';
}
$query = \'SELECT
ID,NUM,FName,LName,AGE,DATEBORN,DDate
FROM
tblBurialRegister WHERE\';
if (!empty($_SESSION[\'firstname\']))
{
$cond = $mydb->prepare(\' FName = %s\', $_SESSION[\'firstname\']);
$query .= $cond;
}
if (!empty($_SESSION[\'lastname\']) && !empty($_SESSION[\'firstname\']))
{
$query .= \' and\';
}
if (!empty($_SESSION[\'lastname\']))
{
$cond = $mydb->prepare(\' LName = %s\', $_SESSION[\'lastname\']);
$query .= $cond;
}
$query .= \' ORDER BY
LName,FName
LIMIT \'
. $limit .
\' OFFSET \'
. $offset .\'\';
$results = $mydb->get_results($query);
// Do we have any results?
if (count($results) > 0) {
echo "<div style=\'overflow-x:auto;\'><table style=\'font-size:large;\'><th>First Name</th><th>Last Name</th><th>AGE</th><th></th>";
foreach ($results as $obj) :
$age = $obj->AGE;
if(empty($age))
{
$age = \'Unknown\';
}
$bdate = $obj->DATEBORN;
if(empty($bdate))
{
$bdate = \'Unknown\';
}
$ddate = $obj->DDate;
if(empty($ddate))
{
$ddate = \'Unknown\';
}
echo "<tr><td>".$obj->FName."</td><td>".$obj->LName."</td><td>".$age."</td><td><form method=\'post\' action=\'http://www.website.com/record-search-details/\'><button id=\'myBtn\' class=\'et_pb_contact_submit et_pb_button rsButton\' style=\'color: #424892!important;
border-color: #424892;\' type=\'submit\' name=\'RecordDetails\' value=".$obj->ID.">Details</button></form></td></tr>";
endforeach;
echo "</table></div>";
} else {
echo \'<p>No results found.</p>\';
}
} catch (Exception $e) {
echo \'<p>\', $e->getMessage(), \'</p>\';
}
此代码自发布以来未被触及,并且一直在运行。在过去的几个月里,他们向我报告分页不起作用,用户无法通过第一页。
在我看来,当单击分页中的链接时,查询字符串正在被删除。?page=2
. 也许WordPress更新中的某些更改改变了查询字符串的行为?
我调查了一下wp_removable_query_args()
在里面functions.php
插件中存在如下内容:
function wp_removable_query_args() {
$removable_query_args = array(
\'activate\',
\'activated\',
\'admin_email_remind_later\',
\'approved\',
\'core-major-auto-updates-saved\',
\'deactivate\',
\'delete_count\',
\'deleted\',
\'disabled\',
\'doing_wp_cron\',
\'enabled\',
\'error\',
\'hotkeys_highlight_first\',
\'hotkeys_highlight_last\',
\'ids\',
\'locked\',
\'message\',
\'same\',
\'saved\',
\'settings-updated\',
\'skipped\',
\'spammed\',
\'trashed\',
\'unspammed\',
\'untrashed\',
\'update\',
\'updated\',
\'wp-post-new-reload\',
);
但我的理解是,这只会删除列出的查询字符串,所以
page
不应移除。任何帮助或指导都将不胜感激。