Jump to content

Don't show pages in navigation


Christoph
 Share

Recommended Posts

Would like to have a checkbox on the Setting tab, maybe in the "status" fieldset, where I can decide to publish a page but not to let it count for the main navigation or sub navigation. I often have pages that just need a link in the content area but no showing up in the any navigation sections.

Link to comment
Share on other sites

Thanks for your reply, Jasper.

The problem with the hidden status is, that you can't find the page in search results. Having a page that you don't want to show up in navigation doesn't mean it also shouldn't be searchable. Maybe this should be separated.

Link to comment
Share on other sites

Pete's solution is probably the best.

Another workaround would be to move the page out of the ordinary pagetree and place it under it's own parent (eg. dontshow). That would allow you to do the following: list all pages in the navigation except those with parent "dontshow".

/Jasper

Link to comment
Share on other sites

I think best solution here would be checkbox field "don't show in navigation" and then just exclude those pages in your navigation selectors. It would be great to have it next to hidden, but that might require module to do so.

Link to comment
Share on other sites

  • 1 month later...

I have a similar problem in that I have a navigation menu featuring a javascript dropdown. Some of the top level pages in the navigation I want to have their children display in the drop down while some I don't. There's a news and events page for example that if all it's children were displayed in the drop down would make for a loooonng menu.

I used Pete's solution by making the pages hidden and including hidden pages in the list on the news and events page BUT...

The whole point of a CMS is to allow people who have no real expertise to edit and add new content and to expect them to remember to mark each new page they create as hidden is asking a lot and I anticipate a big mess that I'll have to clean up. A solution would be to have a way to mark a template as hidden so that all pages created using that template will by default be hidden but that's not possible either.

Having a checkmark field to determine if a page is to be included in drop down navigation is probably a good solution but also requires the user to remember to mark it. Is there an easier way?

Can I add in something to the effect of: if the page is a child of news and events then exclude it? I don't know how you would code that.

Link to comment
Share on other sites

You could do something like

foreach($children as $child) {
 if($child->numChildren && $child->parent!="/news/") {
	// Your code to subpages list your pages here
 }
}

Note: didn't test the code, just written in the browser

Hope this helps!

//Jasper

Link to comment
Share on other sites

Thanks Jasper.

That's the kind of thing I'd like to do but for some reason I can't make it work. I'm using Ryan's excellent listChildrenTree function:

function listChildrenTree($children) {
    echo "<ul>\n";
    foreach($children as $child) {
		    echo "<li><a href='{$child->url}'>{$child->title}</a> ";
		    if($child->numChildren) listChildrenTree($child->children);
		    echo "</li>\n";
    }
    echo "</ul>";
}
listChildrenTree($pages->get('/')->children);

And when I add in your addition it doesn't do anything. If I keep working with processwire i hope I'll eventually be able to figure this stuff out myself but I'm not there yet.

Link to comment
Share on other sites

You can easily exclude pages by any factor that you want to by using a selector when the pages are loaded, or an if() statement after. Jasper was on the right track, but the example he posted wouldn't work just because $child->parent is an object and not a string. However, you could do something like this instead:

foreach($children as $child)
if($child->template == 'some-template') continue; // skip it
// ... otherwise display it ...
}

Another example:

// display children, except for those with parent 'products' or those using template 'employee'
if($child->numChildren && $child->name != 'products' && $child->template != 'employee') {
listChildrenTree($child->children);
}

You can also do something like this:

// list children that aren't using template employee
listChildrenTree($child->children("template!=employee");
Link to comment
Share on other sites

You can easily exclude pages by any factor that you want to by using a selector when the pages are loaded, or an if() statement after. Jasper was on the right track, but the example he posted wouldn't work just because $child->parent is an object and not a string.

Sorry about that, I checked and used the same code on my site, but with one small difference: I used a pageID as the selector and that works of course, since that's a string value

/Jasper

PS: Ryan's last example missed a bracket. Not a big thing, but for the sake of completeness 8) :

// list children that aren't using template employee
listChildrenTree($child->children("template!=employee"));
Edited by formmailer
Link to comment
Share on other sites

My solution is this. I keep list of all templates I want to shown on navigation on my site/config.php file:

$config->navTemplates = 'home|basic-page|news|events';

I have templates like news-item, events-item etc who didn't make to the list.

And whenever I loop normal navs I check against that list:

$templates = $config->navTemplates;
foreach($children("limit=50,template=$templates") as $child) {
...output here
}
  • Like 3
Link to comment
Share on other sites

God you guys are awesome. Thanks. The generosity of everyone who offers insight and helpful tips on this forum is humbling. You guys make me look good to my clients. They think I know what I'm doing. :lol:

I have a template news-list that displays the headlines and dates of each article with a link to go to the page (it's the page that shows up when you click the News and events button in the navigation and a template called news-item which is the template every article uses. So Ryan's approach would work perfectly as I can simply exclude every page using the news-item template.

Works! Just tried it. On to the next thing.

Edited by digitex
  • Like 1
Link to comment
Share on other sites

  • 1 year later...

I thought I would just add my snippet of code that excludes a specific top page from my navigation menu. I used Ryan's example and added the notated line to it. I'm not sure if it is the best way to do it, but it works. 

<?php
        $root = $pages->get("/");
        $children = $root->children();
        $children->prepend($root);
        foreach($children as $child) {
       // Added to remove the site-map from my top menu. Replace site-map with the url
            if($child->name != 'site-map') {
            echo "<li><a href='{$child->url}'>{$child->title}</a></li>";
        }}
  ?>
 
Link to comment
Share on other sites

  • 1 year later...

Hello,

I created check field "skiper" and put it to template. In parent page set it ("check") and later inside navigation loop check if 1 - skip children (hide all of them from menu/submenu).

// rendering submenu items
if ( $item->hasChildren() && $item <> $homepage && !$item->skiper ) {
    
	echo treeMenu (....);  

}

In that way user only need to do one setup, only inside parent page.

Regards.

p.s. sorry if this is not a way to go - today make first steps with PW

Link to comment
Share on other sites

Another workaround would be to move the page out of the ordinary pagetree and place it under it's own parent (eg. dontshow). That would allow you to do the following: list all pages in the navigation except those with parent "dontshow".

then you would have URLs like domain.com/dontshow/sample that's why i don't like this approach.

thank's for bringing up this post, just tried to exclude pages from nav using MarkusSimpleNavigation... something like

'selector' => 'template!=team'

...works like a charm :)

you could also add a checkbox "hidenav" to your templates you want to be able to hide from navigation and then use a selector like this

'selector' => 'hidenav=0'

its as simple as that - i'm amazed once more :)

the complete code for my navigation, just for the record...

<nav class="nav-main mega-menu">
	<?php
	$treeMenu = $modules->get("MarkupSimpleNavigation"); // load the module
	$options = array(
	    'show_root' => true,
	    'outer_tpl' => '<ul class="nav nav-pills nav-main" id="mainMenu">||</ul>',
	    'selector' => 'hidenav=0'
	);
	echo $treeMenu->render($options);
	?>
</nav>
Link to comment
Share on other sites

then you would have URLs like domain.com/dontshow/sample that's why i don't like this approach.

No need for that - in my case parent page is "News", and children are news articles. In menu I have "News" button but don't want to show news articles in submenu.

Just add to parent ("News") check field and it's simple to skip childrens from submenu inside navigation loop.

thank's for bringing up this post, just tried to exclude pages from nav using MarkusSimpleNavigation... something like

'selector' => 'template!=team'

Option with eg. "skiper" check field is that you can use it also on others templates and don't need to edit php.

you could also add a checkbox "hidenav" to your templates you want to be able to hide from navigation and then use a selector like this
'selector' => 'hidenav=0'

Yes that is my option - but I use it different :). Sorry for missunderstanding - I wasn't so clear in my previous post.

In my case (hide children from submenu and also use parent for "listing" view) working solution is this:

add simple check field in parent page and iniside navigation loop (where is children rendering part) ask "if that parent has check field checked" skip children and go to next.

Also same method is for any other page what you want to hide from navigation - only different is place and/or "question" inside navigation loop. 

regards 

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