<?php
function render_links($page, $level = 0) {
echo "<li>";
echo "<a href=''>" . get_the_title($page) . "</a>";
$children = get_pages("child_of=" . $page->id . "&parent=0");
if (!empty($children)) {
echo "<ul>";
foreach ($children as $c) {
render_links($c, $level + 1);
}
echo "</ul>";
}
echo "</li>";
}
echo "<ul>";
foreach (get_pages("parent=0") as $p) {
render_links($top_level);
}
echo "</ul>";
?>
這是我的嘗試呈現出父母和孩子,但它只是循環出現在許多李的當前頁面標題。循環出wordpress菜單項和兒童
任何人都可以幫助我嗎?
UPDATE:
<?php
function render_links($page, $level = 0) {
echo "<li>";
echo "<a href=''>" . get_the_title($page) . "</a>";
$children = get_pages("child_of=" . $page->id);
if (!empty($children)) {
echo "<ul>";
foreach ($children as $c) {
render_links($c, $level + 1);
}
echo "</ul>";
}
echo "</li>";
}
echo "<ul>";
foreach (get_pages("parent=0") as $p) {
render_links($top_level);
}
echo "</ul>";
?>
當嘗試這一點,正確的循環出當前頁面,然後將其修建垃圾一樣的孩子(即使只有1名兒童)。
但是它沒有列出網站上的其他父母頁面。 看圖片: https://gyazo.com/93728738fadc3c6e97ed18d20a46d273
得到這個錯誤就被渲染後:
Fatal error: Maximum function nesting level of '100' reached, aborting! in C:\wamp\www\yangtorp\wp-includes\cache.php on line
UPDATE:
<?php
function render_links($pge, $level = 0) {
echo "<li>";
echo "<a href=''>" . $pge->post_title . "</a>";
$children = get_pages(array('child_of' => $post->ID, 'sort_column' => 'post_date', 'sort_order' => 'desc'));
if (!empty($children) && $level != 2) {
echo "<ul class='sub-menu'>";
foreach ($children as $c) {
render_links($c, $level + 1);
}
echo "</ul>";
}
else{
echo "</li>";
}
}
echo "<ul id='menu-main' class='menu'>";
foreach (get_pages("parent=0") as $p) {
render_links($p);
}
echo "</ul>";
?>
更近一步。現在它列出父頁面,並正確顯示名稱。但是我不能得到這個工作:
$children = get_pages("child_of=" . $page->id . "&parent=0");