Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.