循环wordpress菜单项和儿童

时间:2015-08-31 11:15:18

标签: php wordpress

~ $ checksctp
Sctp supported

这是我尝试渲染父母和子女的,但它只是在很多里面循环出当前的页面标题。

任何人都可以帮助我吗?

更新:

<?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>";
?>

尝试此操作时,它会正确地循环当前页面,然后它会阻止相同的孩子(即使只有1个孩子)。

但是它没有列出网站上的其他父页面。 看图像: https://gyazo.com/93728738fadc3c6e97ed18d20a46d273

渲染后出现此错误:

<?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>";
?>

更新:

Fatal error: Maximum function nesting level of '100' reached, aborting! in C:\wamp\www\yangtorp\wp-includes\cache.php on line

更进一步。现在它列出了父页面,并正确显示了名称。但是我不能让这个工作:

<?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>";
?>

2 个答案:

答案 0 :(得分:2)

我认为你应该这样做:

 if (!empty($children)) {

       echo "<ul>";

   foreach ($children as $c) {
        render_links($c, $level + 1);
    }

    echo "</ul>";
}

 //missing else case
else{
    echo "</li>";
}

答案 1 :(得分:0)

<?php
function render_links($pge, $level = 0) {

    echo "<li>";

    echo "<a href=''>" . $pge->post_title . "</a>";

    //set arguments for which properties and sorting order the children should get.
    $args = array( 
            'child_of' => $pge->ID, 
            'parent' => $pge->ID,
            'hierarchical' => 0,
            'sort_column' => 'menu_order', 
            'sort_order' => 'asc'
    );

    // throw the arguments into the get_pages query
    $children = get_pages($args);

 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>";
?>