【发布时间】:2017-05-22 01:17:25
【问题描述】:
我正在尝试创建一个动态抓取特定页面的所有子页面的函数。然后它会检查当前页面是什么,并抓取接下来的三个页面以显示为预览。
最后一页 ID 是 6120。 您可以对 $pagesArray() 以及 $lastKey 和 $matches 进行硬编码以查看结果。
$pagesArray(
[0] => 6135
[1] => 6139
[2] => 6176
[3] => 6178
[4] => 6163
[5] => 6152
[6] => 6167
[7] => 6183
[8] => 6201
[9] => 6190
[10] => 6172
[11] => 6197
[12] => 6205
[13] => 6154
[14] => 6120
);
$lastKey = 14;
$matches = 14;
当我转到最后一页时,页面 ID 是 6120,这是数组的第 14 个 ID。 它应该返回前 3 个页面 ID,但它不会匹配。请帮忙。这是代码。 作为参考,$matches == $lastKey 应该是设置 $nextPagesArray() 的条件的一部分。他们都是 14 岁,但不匹配。
function getNext3PortfolioPages(){
global $post;
$currentPage = $post->ID;
$last = '';
$lastKey = '';
$pagesArray = array();
$nextPagesArray = array();
$args = array(
'child_of' => '6115',
'offset' => $post->ID
);
// get all the page id's that are children of page 6115
$result = get_pages($args);
// add the page id's to the $pagesArray
foreach ( $result as $page ){
array_push($pagesArray, $page->ID);
}
//find the last page id in the array
$last = end($pagesArray);
// find the index of the last array value
$lastKey = key($pagesArray);
// find the index of the current page displayed
$matches = array_search($currentPage, $pagesArray);
// check to see if the current page index is the same as the end of the array
// and set up the $nextPagesArray with the next 3
if ( (int)$matches == (int)$lastkey ) {
//print_r('1');
//print_r($pagesArray);
array_push($nextPagesArray, $pagesArray[0]);
array_push($nextPagesArray, $pagesArray[1]);
array_push($nextPagesArray, $pagesArray[2]);
} elseif ( $matches == $lastkey - 1 ) {
//print_r('2');
array_push($nextPagesArray, $pagesArray[$lastKey]);
array_push($nextPagesArray, $pagesArray[0]);
array_push($nextPagesArray, $pagesArray[1]);
} elseif ( $matches == $lastkey - 2 ) {
//print_r('3');
array_push($nextPagesArray, $pagesArray[$lastKey - 1]);
array_push($nextPagesArray, $pagesArray[$lastKey]);
array_push($nextPagesArray, $pagesArray[0]);
} else {
//print_r('4');
array_push($nextPagesArray, $pagesArray[$matches + 1]);
array_push($nextPagesArray, $pagesArray[$matches + 2]);
array_push($nextPagesArray, $pagesArray[$matches + 3]);
}
return $nextPagesArray;
}
【问题讨论】: