Jump to content

Navigation Disaster


louisstephens
 Share

Recommended Posts

Well, after I thought I was near completion, a fun issue reared its ugly head. I have my tree set up like (names are just examples):

Home

Articles

- User 1

-- Article 1

-- Article 2

-- Article 3

-- Article 4

 - User 2

-- Article 1

-- Article 2

-- Article 3

-- Article 4

 

I have a drop down in my navigation called "User 1" (for when you are under User 1) that uses:

<?php
					foreach($page->siblings as $child) {
    					echo "<li><a href='{$child->url}'>{$child->title}</a></li>";
					}
				?>

This works great as long as you Stay in user 1. However if you go to the "User 2" dropdown, everything is replaced by user 2 and user 1 no longer displays. I realize it is because I am selecting the siblings, but is there a way to go about haveing both dropdowns contained and populated no matter which article I am currently viewing?

Link to comment
Share on other sites

If I understand well, you want to have a dropdown for each user. If so, you could do this:

foreach($pages->get("/articles/")->children as $writer) {

    echo "<ul>"; // your dropdowns are lists, right?

    foreach($child as $article) {
        echo "<li><a href='{$child->url}'>{$child->title}</a></li>";
    }

    echo "</ul>";
}
Link to comment
Share on other sites

Hmm, is there a way to achieve this without selecting a parent? The template was going to be used on multiple sections like:

Articles

- User 1

-- Article 1

-- Article 2

-- Article 3

-- Article 4

 

 - User 2

-- Article 1

-- Article 2

-- Article 3

-- Article 4

Events

- User 1

-- Event 1

-- Event 2

-- Event 3

-- Event 4

 - User 2

-- Event 1

-- Event 2

-- Event 3

-- Event 4

Sorry, I know this is a bit confusing.

Link to comment
Share on other sites

Sorry if I have been confusing (big learning opportunity for me to get comfortable with php), this is what I have for my nav. When I used what you showed I end up with a blank dropdown list. I understand that the foreach loops through the root parent which would show just "Articles" and "Events" ( need to populate with article 1 article 2 etc etc) and have it stay on each page no matter where you are within Articles and Events.

<nav class="navbar navbar-default navbar-static-top" data-offset-bottom="0" id="navbar">
  <div class="container">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse container" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
		<li><a href="">Home</a></li>
        <li class="dropdown">
          <a href="" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Articles <span class="caret"></span></a>
          <ul class="dropdown-menu">
           		<?php
					foreach($page->siblings as $child) {
    					echo "<li><a href='{$child->url}'>{$child->title}</a></li>";
					}
			  
			  
				?>
          </ul>
        </li>
		
		<li class="dropdown">
          <a href="" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Events <span class="caret"></span></a>
          <ul class="dropdown-menu">
           		<?php
					foreach($page->siblings as $child) {
    					echo "<li><a href='{$child->url}'>{$child->title}</a></li>";
					}
			  
			  
				?>
          </ul>
        </li>
        <li><a href="">Link</a></li>
		<li><a href="">Link</a></li>
		<li><a href="">Link</a></li>
		<li><a href="">Link</a></li>
		<li><a href="">Link</a></li>
      </ul>

      <ul class="navbar-right social">
        
	</ul>
            
                        
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>
</div> 
Link to comment
Share on other sites

Sorry, I'm on my phone so bare with me.

 

$results = $pages->get("/")->children("template=events|articles");
foreach($results as $result) {
   echo $result->title;
   foreach($results->children as $child) {
     echo $child->title;
     foreach($child->children as $child) {
       echo $child->title;
     }
   }
}
Edited by kongondo
wrapped code in code block
  • Like 1
Link to comment
Share on other sites

Thanks everyone for the speedy assistance! Just curious, is it possible to inc. a if/else statement in a for each statement? Ie:

$results = $pages->get("/")->children(

if {

"template=events"
}

else if {

"template=articles"
}

else {

"template=someothertemplate"
}


);

I just realized that I am might have many different instances of this on other templates

Link to comment
Share on other sites

Thanks Horst. I went with the switch statement, but I think I may have hit a block here.

<?php
	foreach($page->siblings as $child) {
    	switch($child->template) {
			case 'articles':
				echo "<li><a href='{$child->url}'>{$child->title}</a></li>";
				break;
        					
			default:
			
			}
		}
			  
?>

<?php
	foreach($page->siblings as $child) {
    	switch($child->template) {
			case 'events':
				echo "<li><a href='{$child->url}'>{$child->title}</a></li>";
				break;
        	
			default:
			
			}
		}
			  
?>

I had tried using siblings to select, but I now see that it causes my biggest issue. When you are on one of the articles, it does pull in the other articles in the drop down. However, the events dropdown displays nothing. I understand that it is the "siblings" causing this. However, I really need the pages to display no matter where you are. Did I just miss a simple selector?

Link to comment
Share on other sites

$page is always relative to the current page. So if you are asking for the siblings of the current page, it would do just that. Instead you need to use get, to make sure that parent is always the same. To do this you use $pages->get()->siblings inside the () add your selector.

  • Like 1
Link to comment
Share on other sites

So just so I understand this, you are saying something like:

$pages get(parentName)->siblings as $child

How would this work though if you were dynamically trying to code this if there would be multiple "parentNames"? Sorry, I am just trying to get a grasp on all of this. I appreciate your help @tom @horst and everyone else. Thank you a bunch

Link to comment
Share on other sites

So just so I understand this, you are saying something like:

$pages get(parentName)->siblings as $child

How would this work though if you were dynamically trying to code this if there would be multiple "parentNames"? Sorry, I am just trying to get a grasp on all of this. I appreciate your help @tom @horst and everyone else. Thank you a bunch

This is cleared by the answer written by Tom.

Sorry, I'm on my phone so bare with me.

$results = $pages->get("/")->children("template=events|articles");
foreach($results as $result) {
   echo $result->title;
   foreach($results->children as $child) {
     echo $child->title;
     foreach($child->children as $child) {
       echo $child->title;
     }
   }
}

I will elaborate on his answer just trying to include the HTML you wrote a few posts back, I hope it makes it more clear. I'm not exactly sure the syntax of the nested dropwdowns is correct, hope you get the idea.

<?php $results = $pages->get("/")->children("template=events|articles"); 
//$result is one of your "content types", in this example, it could be Articles or Events page object.
?>
<?php foreach ($results as $result): ?> 
  <li class="dropdown">
    
    <a href=""
   class="dropdown-toggle"
   data-toggle="dropdown"
   role="button" aria-haspopup="true"
   aria-expanded="false"><?php echo $result->title //This would echo either "Article" or "Events" ?>
      <span class="caret"></span>
    </a>
    
    <ul class="dropdown-menu">
      <?php foreach($result->children as $user):?>
        <li><a href='<?php echo $user->url ?>'><?php echo $user->title ?></a>
          <?php foreach($user->children as $item): //This would be an article or event in itself?> 
              <li><a href='<?php echo $user->url ?>'><?php echo $user->title ?></a>
          <?php endforeach; ?>
          </li>
     <?php endforeach;?>
    </ul>
</li>
<?php endforeach; ?>

This could be included anywhere in any template and render the same, because $pages (whatch out for that plural!) variable that let's retrieve any page no matter where you are on the tree. The $pages->get() retrieves one page, in this case the root page, and the next method "children", returns the PageArray whose contained pages matches the selector.

The only thing I don't like about this is that I would need to specify the selector's parameters in case I add more "content types". But that's more of a topic on how you would plan your website. 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...