ragrim Posted September 9, 2011 Share Posted September 9, 2011 Hi, Im building a custom theme for wordpress and i need some help with a loop, its not wordpress specific code just a general if and loop. heres what i have. $pages = get_pages('parent=0&sort_column=menu_order'); if ($pages) { $output=''; foreach ($pages as $page) { if($page->post_status=='publish'){ $output.='<li class=top><a href="'.$page->guid.'" class=top_link><span>'.$page->post_title.'</span></a>'; $sub_pages = get_pages( 'child_of ='. $page->ID); if ($sub_pages){ foreach ($sub_pages as $sub_page) { if($sub_page->post_status=='publish'&& $sub_page->post_parent== $page->ID) { if($sub_page->guid==" ") { } else { $output.='<ul class=sub>'; $output.='<li><a href="'.$sub_page->guid.'">'.$sub_page->post_title.'</a></li>'; $output.='</ul>'; } } } }} $output.='</li>'; } } What im trying to do is only echo <ul class=sub> and the inside <li> if there is something in sub_page guid. I managed to build this code and it works, but i need to move the <ul></ul> outside of the loop so it doesnt repeat over and over again, but i also dont want it being echoed if sub_page->guid is empty sorry for my poor explanation im doing the best i can. Thanks in advance for any help Link to comment https://forums.phpfreaks.com/topic/246776-need-help-with-if-statement-and-loop/ Share on other sites More sharing options...
Adam Posted September 9, 2011 Share Posted September 9, 2011 Store the HTML in a separate variable, and if it's not empty after the loop then concatenate it onto $output wrapped in the UL tags. Link to comment https://forums.phpfreaks.com/topic/246776-need-help-with-if-statement-and-loop/#findComment-1267304 Share on other sites More sharing options...
ragrim Posted September 9, 2011 Author Share Posted September 9, 2011 Thanks for the reply, im sorry to ask but are you able to provide an example, ive never done anything like you have suggested. Thanks. Link to comment https://forums.phpfreaks.com/topic/246776-need-help-with-if-statement-and-loop/#findComment-1267308 Share on other sites More sharing options...
Adam Posted September 9, 2011 Share Posted September 9, 2011 $list_output = ''; foreach ($sub_pages as $sub_page) { if ($sub_page->post_status=='publish' && $sub_page->post_parent== $page->ID && !empty($sub_page->guid)) { $list_output .= '<li><a href="'.$sub_page->guid.'">'.$sub_page->post_title.'</a></li>'; } } if (!empty($list_output)) { $output .= '<ul class="sub">' . $list_output . '</ul>'; } Link to comment https://forums.phpfreaks.com/topic/246776-need-help-with-if-statement-and-loop/#findComment-1267309 Share on other sites More sharing options...
ragrim Posted September 9, 2011 Author Share Posted September 9, 2011 Thank you very much, thats fixed it perfectly! Much appreciated. Link to comment https://forums.phpfreaks.com/topic/246776-need-help-with-if-statement-and-loop/#findComment-1267314 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.