[英]PHP and Concrete 5 count subpages
使用Concrete5制作移動網站,並使用帶有自定義模板的頁面列表塊。 我正在嘗試使用PHP計算子頁面。
<?php foreach ($pages as $page):
// Prepare data for each page being listed...
$title = $th->entities($page->getCollectionName());
$url = $nh->getLinkToCollection($page);
$target = ($page->getCollectionPointerExternalLink() != '' && $page->openCollectionPointerExternalLinkInNewWindow()) ? '_blank' : $page->getAttribute('nav_target');
$target = empty($target) ? '_self' : $target;
$description = $page->getCollectionDescription();
$description = $controller->truncateSummaries ? $th->shorten($description, $controller->truncateChars) : $description;
$description = $th->entities($description);
$mies = 0;
?>
<li class="ui-btn ui-btn-icon-right ui-li-has-arrow ui-li ui-btn-up-c" data-theme="c"><div aria-hidden="true" class="ui-btn-inner ui-li"><div class="ui-btn-text"><a target="<?php echo $target ?>" class="ui-link-inherit" href="<?php echo $url ?>">
<h2 class="ui-li-heading"><?php echo $title ?></h2>
<p class="ui-li-desc"><?php echo $description ?></p>
</a>
</div><span class="ui-icon ui-icon-arrow-r ui-icon-shadow"></span><span class="ul-li-count ui-btn-corner-all ul-count-second"><?php echo count($mies) ?></span></div></li>
<?php endforeach; ?>
因此,我可能不知道需要使用Count函數(或長度嗎?)。 如果我在錯誤的地方編輯,請告知您在Concrete5 cms方面的經驗。
如果要在代碼的span
元素中顯示相應的頁碼:
<span class="ul-li-count ui-btn-corner-all ul-count-second"><?php echo $mies; ?></span>
如果要顯示其余的子頁面,則在上面的html代碼段中,只需將$mies
替換$mies
count($pages) - $mies
像這樣:
<span class="ul-li-count ui-btn-corner-all ul-count-second"><?php echo count($pages) -$mies; ?></span>
在開始for
循環之前,您首先必須初始化$mies
,所以它應該是以下形式:
<?php
$mies = 0;
foreach ($pages as $page):
//Rest of your code and increment $mies with every iteration
$mies ++; //This ensures that you are counting the corresponding pages
?>
如果要獲取子頁面總數的計數, $mies
在for塊外回顯$mies
即可,例如:
<?php
endforeach;
echo $mies; //Shows you the total number of pages processed inside the for loop.
//or Just find the length of pages array
echo count($pages);
?>
至於獲取數組的長度,您可以使用count
或sizeof
。 我偶然發現一個SO問題有關使用count
或sizeof
方法用於查找一個陣列的長度。
這應該使您朝正確的方向開始。
您需要父母ID;
$parentId = Page::getCollectionParentId();
請注意, Page::getCollectionParentId()
獲取當前頁面的父ID,因此您可能需要嘗試;
$parentId = $page->getCollectionParentID();
然后創建一個新的PageList進行過濾,並使用parentId進行過濾;
Loader::model('page_list');
$pl = new PageList();
$pl->filter(false, '(p1.cParentID IN ' . $parentId . ')');
// Get the total
var_dump($pl->getTotal());
這未經測試,但該理論是有道理的。
這可能會更簡單。
$pl->getTotal()
$pl
是在控制器中設置的PageList
對象。
另外,這些天,您可以只使用h()
方法,而不用寫出$th->entities()
編輯:我應該澄清一下,您不需要執行$pl = new PageList()
因為$pl
已經設置為控制器中的PageList
對象,並已傳遞給視圖。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.