Jump to content

Drop-Down Menu not showing on back pages


spercy16
 Share

Recommended Posts

I'm having an issue where my navigation bar isn't showing up on the back pages of my site. I am new to ProcessWire and definitely don't have it all figured out yet. If someone could explain why it isn't working on my child pages and provide me with an updated snippet that will work on those pages I would really appreciate it. Here's the code I'm using on my home page:

<?php function listChildrenTree($children)
		{
			echo "<ul class='w-full text-white lg:block lg:float-left'>";
			foreach ($children as $page) {
				echo "<li class='relative w-full lg:w-auto'>
							<a href='{$page->url}'>
								<div onclick='showSub()' class='block pt-2 pb-3 pl-6 lg:pr-6 button'>
									{$page->title}
								</div>
							</a>
							";
				if ($page->numChildren) listChildrenTree($page->children);
				echo "</li>";
			}
			echo "</ul>";
		}
		listChildrenTree($pages->get('/')->children);
?>
Link to comment
Share on other sites

2 hours ago, spercy16 said:

foreach ($children as $page) {

$page is the API variable for the current page you are on, so you can not redefine it here. Better take

foreach ($children as $child) {

 

Link to comment
Share on other sites

That didn't help me at all. Like I said I'm new to ProcessWire.... I tried replacing that statement with yours, as well as the other references to $page (because they were "undefined variables" after changing the first statement) and the menu still didn't show up on my child pages.

Link to comment
Share on other sites

29 minutes ago, spercy16 said:

That didn't help me at all.

I didn't say that would solve your problem. But it was necessary anyway.

I'm not sure that how you're calling the function recursively is correct.

Link to comment
Share on other sites

Hi @spercy16,

First of all, Please be kind to someone who just want to help you out even his answer is not that you want or need. Generally speaking the ProcessWire community is very kind and willing to help. I am sure you will have your help if you are kind and patient enough.

For the code, @ottogal is right. You call the listChildrenTree function inside itself. This is a fatal error and your code will never work until you fix it.

You cannot use $page here. $page is the reserved variable that represents the current page object. You may use $child instead.

foreach ($children as $page) {

Like I said above, don't call the same function within itself.

if ($page->numChildren) listChildrenTree($page->children);

The complete code:

echo "<ul class='w-full text-white lg:block lg:float-left'>";
			foreach ($children as $child) {
				echo "<li class='relative w-full lg:w-auto'>
							<a href='{$child->url}'>
								<div onclick='showSub()' class='block pt-2 pb-3 pl-6 lg:pr-6 button'>
									{$child->title}
								</div>
							</a>
							";
				if ($child->numChildren) {
  					echo "<ul>";
  					foreach ($child->children as $grandchild) {
					echo "<li class='relative w-full lg:w-auto'>
							<a href='{$grandchild->url}'>
								<div onclick='showSub()' class='block pt-2 pb-3 pl-6 lg:pr-6 button'>
									{$grandchild->title}
								</div>
							</a>
							";
  					echo "</li>";
  					}
  				echo "</ul>";
				echo "</li>";
			}
			echo "</ul>";
		

Hope this helps or someone can improve the answer.

Gideon

Link to comment
Share on other sites

6 hours ago, spercy16 said:

If someone could explain why it isn't working on my child pages and provide me with an updated snippet that will work on those pages I would really appreciate it.

Hi @spercy16,

Your snippet is working just fine for me. I am not sure what you mean by not working in my child pages but I think the reason is access issues. The following will not naturally show up in page finds:

  1. Unpublished pages
  2. Pages the user has no access to
  3. Hidden pages

If your pages (including child ones) fall into any of those categories, they will not show up. However, if you need them to, you can override access control behaviours. For something like a menu, that may not always be what you want. For instance, if a page is unpublished, you want it that way for a reason. Please have a look at the docs (link below) for how you can use (for example, $pages->get('/')->children('include=unpublished')) overrides in your selectors.

https://processwire.com/docs/selectors/#access_control

Since you are new to ProcessWire, please note that overriding access controls using 'include=all' for the children of Home ('/') means you are asking ProcessWire to return all pages on the site, including admin pages, roles, permissions, etc, clearly not what you want. Additionally, calling $pages->get('/')->children without any limits, [e.g. $pages->get('/')->children('limit=50'))] can potentially lead to the retrieval of lots of pages (assuming you have a big site).

A quick by the way, although this:

foreach ($children as $page)

does not affect the working of the function, since the $page variable is out of scope inside a function, it's just good to know that if used outside, it would have side-effects, i.e. overwriting the current page (variable). Maybe you knew this already. Otherwise, conventionally, some people go for $children as $child or $children as $c or $children as ....whatever, for consistency (and clarity) of use in and out of functions ?.

Welcome to ProcessWire and the forums ? 

  • Like 2
Link to comment
Share on other sites

8 minutes ago, Gideon So said:

You call the listChildrenTree function inside itself. This is a fatal error and your code will never work until you fix it.

Hi @Gideon So. Thanks for chiming in. I see we posted at the same time.

Please let me state that this is incorrect. @spercy16's function is a recursive function ? It can be called inside itself. The code works just fine as it is (I tested it) ? 

  • Like 2
Link to comment
Share on other sites

My objective wasn't to be rude, it was to kindly ask people like him to not respond if his post doesn't help solve the question posted. I respond to questions if I don't know the answer and would appreciate the same from other people. It clutters up forums when people start responding with answers that aren't correct or don't fix the problem.

I just tried your code on my one of my child pages and the menu still does not show up....

Link to comment
Share on other sites

Actually the code was fine. I checked the source code of the page and realized that the menu was being output from the php just as it was on my home page. I realized that it wasn't being displayed because Jquery was loading on my home page (through my billboard include file) but not on my back page, while I am using it (Jquery) to hide and show my menu.... LOL Thanks for all of your help anyways!

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...