Jump to content

need help with if statement and loop


ragrim

Recommended Posts

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

        $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>';
        }

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.