Divide foreach into multiple divs

I have this functions which output 10 links, I would like to divide links into two divs, but I think echo get lost when I call the function.

This is what I have:

Read More
            function wpb_list_child_pages() { 

            $args = array(
            'sort_order' => 'asc',
            'sort_column' => 'post_title',
            'hierarchical' => 1,
            'meta_value' => '',
            'child_of' => 108,
            'parent' => -1,
            'exclude_tree' => '',
            'offset' => 0,
            'post_type' => 'page',
            'post_status' => 'publish'
            ); 
            $pages = get_pages($args); 


            if ( $pages) 
            {   
            $links = '';
            $counter = 1;
            foreach($pages as $page):

            $links .= '<ul> <li> <a href=" ' .  get_page_link( $page->ID ) . '"> ' .$page->post_title . ' </a> </li></ul>';
            $counter++; // increment the counter variable for each loop
            endforeach; 

            return $links;

            }

            }

And this is why I have tried to do:

            function wpb_list_child_pages() { 

            $args = array(
            'sort_order' => 'asc',
            'sort_column' => 'post_title',
            'hierarchical' => 1,
            'meta_value' => '',
            'child_of' => 108,
            'parent' => -1,
            'exclude_tree' => '',
            'offset' => 0,
            'post_type' => 'page',
            'post_status' => 'publish'
            ); 
            $pages = get_pages($args); 


            if ( $pages) 
            {   
            $links = '';
            $counter = 1;
            foreach($pages as $page):
            if($counter == 1) {
            echo "<div class='left_class'>"; // open left div
            }
            $links .= '<ul> <li> <a href=" ' .  get_page_link( $page->ID ) . '"> ' .$page->post_title . ' </a> </li></ul>';
            //var_dump ($links);
            if($counter==5){
            echo "</div>"; //close left div
            echo "<div class='right_class'>"; // open right div
            }

            if($counter==10){
            echo "</div>"; //close right div
            }
            $counter++; // increment the counter variable for each loop
            endforeach; 

            return $links;

            }                               
            }

and this is how I call the function

 echo '<div class="section-1">'.wpb_list_child_pages().'</div>';

Related posts

1 comment

  1. Your script seems to collect the links in variable before it outputs it. Your (poorly indented) example should look like this:

            function wpb_list_child_pages() { 
    
            $args = array(
            'sort_order' => 'asc',
            'sort_column' => 'post_title',
            'hierarchical' => 1,
            'meta_value' => '',
            'child_of' => 108,
            'parent' => -1,
            'exclude_tree' => '',
            'offset' => 0,
            'post_type' => 'page',
            'post_status' => 'publish'
            ); 
            $pages = get_pages($args); 
    
    
            if ( $pages) 
            {   
            $links = '';
            $counter = 1;
            foreach($pages as $page):
            if($counter == 1) {
            $links .= "<div class='left_class'>"; // open left div
            }
            $links .= '<ul> <li> <a href=" ' .  get_page_link( $page->ID ) . '"> ' .$page->post_title . ' </a> </li></ul>';
            //var_dump ($links);
            if($counter==5){
            $links .= "</div>"; //close left div
            $links .= "<div class='right_class'>"; // open right div
            }
    
            if($counter==10){
            $links .= "</div>"; //close right div
            }
            $counter++; // increment the counter variable for each loop
            endforeach; 
    
            return $links;
    
            }                               
            }
    

    So, what went wrong is you echoed all your (opening and closing) tags before you echoed the links.

Comments are closed.