今天问了AI问出来了父子页面输出代码!记录一下!
输出网站所有父页面列表
<?php $this->widget('Widget_Contents_Page_List')->to($pages); ?>
<?php while($pages->next()): ?>
<?php if($pages->parent == 0): // 只显示父级页面 ?>
<li>
<a href="<?php $pages->permalink(); ?>"><?php $pages->title(); ?></a>
</li>
<?php endif; ?>
<?php endwhile; ?>
输出当前页面的一级子页面列表
<?php
// 获取当前页面ID(适用于页面模板中)
$currentPageId = $this->cid;
$this->widget('Widget_Contents_Page_List')->to($pages);
?>
<?php while($pages->next()): ?>
<?php if($pages->parent == $currentPageId): // 筛选当前页面的直属子页面 ?>
<li>
<a href="<?php $pages->permalink(); ?>"><?php $pages->title(); ?></a>
</li>
<?php endif; ?>
<?php endwhile; ?>
输出本站所有页面列表(有层级)
代码一
<?php
// 获取当前页面ID
$currentPageId = $this->cid;
// 获取所有页面并按父级ID分组存储
$this->widget('Widget_Contents_Page_List')->to($allPages);
$pageTree = [];
while ($allPages->next()) {
$pageTree[$allPages->parent][] = [
'id' => $allPages->cid,
'title' => $allPages->title,
'link' => $allPages->permalink
];
}
// 递归构建嵌套列表函数
function buildTree($parentId, $tree, $depth = 0) {
if (!isset($tree[$parentId])) return;
$indent = str_repeat(' ', $depth*2); // 缩进格式
echo "{$indent}<ul class='level-{$depth}'>\n";
foreach ($tree[$parentId] as $page) {
echo "{$indent} <li>\n";
echo "{$indent} <a href='{$page['link']}'>{$page['title']}</a>\n";
buildTree($page['id'], $tree, $depth+1);
echo "{$indent} </li>\n";
}
echo "{$indent}</ul>\n";
}
// 执行输出
buildTree($currentPageId, $pageTree);
?>
代码二
<?php
$currentPageId = $this->cid;
$this->widget('Widget_Contents_Page_List')->to($pages);
$pageMap = [];
// 构建页面关系映射
while($pages->next()) {
$pageMap[$pages->cid] = [
'parent' => $pages->parent,
'title' => $pages->title,
'link' => $pages->permalink
];
}
// 递归查找所有子页面
function findAllChildren($parentId, $map, $depth = 0) {
foreach ($map as $id => $page) {
if ($page['parent'] == $parentId) {
echo str_repeat('— ', $depth); // 用横线表示层级
echo "<a href='{$page['link']}'>{$page['title']}</a><br>";
findAllChildren($id, $map, $depth+1);
}
}
}
findAllChildren($currentPageId, $pageMap);
?>
没了!😶