Looping out wordpress menu items and childrens

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

This is my attempt to render out parent and childrens, however it is only looping out the current page title in lots of li’s.

Can anyone help me?

Read More

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

When trying this, it correctly loops out current page and then it spams the same children(even if there only is 1 children).

However it does not list the other parent pages on the website.
Look image:
https://gyazo.com/93728738fadc3c6e97ed18d20a46d273

Getting this error after it being rendered:

Fatal error: Maximum function nesting level of '100' reached, aborting! in C:wampwwwyangtorpwp-includescache.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>";
?>

One step closer. Now it lists the parent pages, and displays the name correctly. However I cant get this to work:

$children = get_pages("child_of=" . $page->id . "&parent=0");

Related posts

2 comments

  1. I think you should do like this :

     if (!empty($children)) {
    
           echo "<ul>";
    
       foreach ($children as $c) {
            render_links($c, $level + 1);
        }
    
        echo "</ul>";
    }
    
     //missing else case
    else{
        echo "</li>";
    }
    
  2. <?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>";
    ?>
    

Comments are closed.