Jump to content
photoman355

Page as "Home" navigation setup?

Recommended Posts

I have a slightly unusual setup where I'm using the first child of the root as my homepage like so:
 
Root
      Home
      Page 2
      Page 3 
      etc
 
This is presenting me with a slight problem in that I need the Home url in the navigation tree to redirect to Root.  Here's my navigation setup:
 
<?php
$root = $pages->get("/");
$children = $root->children();
foreach($children as $child) {
    echo "<li><a href='#{$child->name}'></a><a href='{$child->url}'><h1>{$child->title}</h1></a></li>";
}
?>

I'm sure there's a nice easy way to do this in PW, can anyone help?

For data setup I have Home rendered in the Root page.  Looking at SEO would simply changing the menu structure be enough to stop robots indexing the root/Home url so that Home is seen as Root?  

Share this post


Link to post
Share on other sites

There is. Straight from the Basic Profile that comes with ProcessWire:

$homepage = $pages->get("/"); 
$children = $homepage->children;
$children->prepend($homepage); 

foreach($children as $child) {
	$class = $child === $page->rootParent ? " class='on'" : '';
	echo "<li><a$class href='{$child->url}'>{$child->title}</a></li>";
}

Edit: I'm reading your question again and I'm not sure this is helping you.

Wouldn't it be better to rename Root to Home and delete the current Home living under Root?

Edit2: I'm now reading the first sentence of your question and finding myself completely useless...

Edited by MichaMichaMicha

Share this post


Link to post
Share on other sites

 There is. Straight from the Basic Profile that comes with ProcessWire:

$homepage = $pages->get("/"); 
$children = $homepage->children;
// $children->prepend($homepage); 

foreach($children as $child) {
	$class = $child === $page->rootParent ? " class='on'" : '';
	echo "<li><a$class href='{$child->url}'>{$child->title}</a></li>";
}

Hhm, I think there is already the solution within the Basic Profile as you shown above.

But you should not prepend the $homepage.

This will output :

 
      Home
      Page 2
      Page 3 
      etc
 
is this what it should be?

Share this post


Link to post
Share on other sites
I have a slightly unusual setup where I'm using the first child of the root as my homepage like so:
 
Root
      Home
      Page 2
      Page 3 
      etc
 
This is presenting me with a slight problem in that I need the Home url in the navigation tree to redirect to Root.  

For data setup I have Home rendered in the Root page.  Looking at SEO would simply changing the menu structure be enough to stop robots indexing the root/Home url so that Home is seen as Root?  

How I understand it....

When user clicks on "Home" he should be redirected to "Root".

I suggest to put the redirect in the Template for Home to redirect to Root. This means Home will still have unique "Home" url but redirection will happen. Or else, if you want Home to have Root's address in the menu itself, then you will need to use some if statement....

I hope I understood you :)

Share this post


Link to post
Share on other sites

May I ask the question: Why this setup? :)

Makes no sense for me if you are redirecting Home to Root, why do you need Home?

  • Like 1

Share this post


Link to post
Share on other sites

Many thanks guys.  I knew about the prepend option but it doesn't solve the problem because then you get this structure in the nav:

Root

Home

Page 1

Page 2

Root's name should be the name of the "Home" page.  I have come up with a solution for this which solves the menu problem however it doesn't create a permanent redirect for the root/home url and it breaks my scrollspy for some reason.  

<?php
	$root = $pages->get("/");
	$child = $root->child();
	if ($child) { echo "<li class='#{$child->name}'></a><a href='{$root->url}'><h1>{$child->title}</h1></a></li>";
	} 
	$root = $pages->get("/");
	$first = $root->child();
	$children = $root->children()->not($first);
	foreach($children as $child) {
		echo "<li class='#{$child->name} {$active}'></a><a href='{$child->url}'><h1>{$child->title}</h1></a></li>";
	}
?>

@kongondo's Your suggestion of putting the redirect in the Template for Home makes a lot of sense but I still need to pull in the code from Home to the Root page so would need to bear that in mind.  I'd definitely need the menu to tie up.  Can you suggest how I'd go about this?  From an SEO point of view is changing the menu enough to stop robots finding the root/Home url?

@Wanze I know it's a bit of a strange setup.  I have several pages as content blocks so putting them in the backend structure as suggested could get messy from a client point of view.  If Root was Home it would look something like this:

Root

    Block 1

    Block 2

    Block 3

    Page 1

        Block 1

        Block 2

    Page 2

        Block 1

    etc

Share this post


Link to post
Share on other sites

Just wanted to mention you should change this:

$child = $root->child();
if ($child) {
To this: 
$child = $root->child();    
if ($child->id) {

Share this post


Link to post
Share on other sites

@kongondo's Your suggestion of putting the redirect in the Template for Home makes a lot of sense but I still need to pull in the code from Home to the Root page so would need to bear that in mind.  I'd definitely need the menu to tie up.  Can you suggest how I'd go about this?  From an SEO point of view is changing the menu enough to stop robots finding the root/Home url?

You can use PW $page->render() method to pull in the content of Home into the Root page. See cheat sheet and/or search forums for more. You could also just use normal PW variables to output certain fields from "Home" into the Root page. If you use render, you want to make sure the HTML in the Home page is minimal (render will pull in the whole markup so you may end up with duplicate body, head, etc. tags, i.e. Root's and Home's :).

As for SEO, I don't know what effect this would have, sorry.

Share this post


Link to post
Share on other sites

Just wanted to mention you should change this:

$child = $root->child();
if ($child) {
To this: 
$child = $root->child();    
if ($child->id) {

Do you mean the correct way to check if a child is valid is to check for it's id?

Why doesn't the other method work?

Share this post


Link to post
Share on other sites

Do you mean the correct way to check if a child is valid is to check for it's id?

Why doesn't the other method work?

If Pw does not find a page, a NullPage object is returned. The id of a NullPage is always '0'.

So the simplest way to check if you got a page back is to check the id.

Just checking if $child is true is wrong, because it returns true also if $child is a NullPage.

see http://www.php.net/manual/en/language.types.boolean.php for a list of things that are converted to false by php.

  • Like 2

Share this post


Link to post
Share on other sites

alternative solution:

if(empty($child)) {

or

if( ! empty($child)) {



EDIT: (after Ryans post below)

oh, f**k, it is wrong. (I have to check my code)

Edited by horst
  • Like 1

Share this post


Link to post
Share on other sites

alternative solution:

if(empty($child)) {

or

if( ! empty($child)) {
 
 
 
I don't think this would work as I'm not aware of any situation in PHP 5+ where PHP will return a TRUE value for empty() on an object. Assuming I understand correctly, PHP will never return anything but FALSE on an object. Though I think that in PHP 4 it would return TRUE for an object with nothing in it, but not so in PHP 5.
  • Like 1

Share this post


Link to post
Share on other sites

Oh. - thanks Ryan.
 
I have checked at php.net and it is noted in Changelog since PHP 5.0.0 :-[

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By crusin
      hey guys we are using processwire version 3.0.42, we have an important notice that we want to show on the home page of our website. its an image and it should load once the website is loaded and there should be an option to close the popup appearing. is there any plugin that can help us get this done or some code any kind of help would be useful. its a litle urgent.
      thanks in advance.
    • By stanoliver
      Hi! 
      The following code snippet is part of my markup simple navigation and the url_redirect (url field in the backend) just works fine when I put an special custom url into the url_redirect field.
      <?php $nav = $modules->get("MarkupSimpleNavigation"); // topnav $current = $page->rootParent(); // current root page // subnav echo $nav->render(array( 'max_levels' => 2, 'item_tpl' => '<h4><a class="g8-bar-item g8-hover-black" href="{url_redirect|url}">{title}</a></h4><hr class="sidenav">', 'collapsed' => true, ), $page, $current); ?>  In my seperated breadcrumb navigation I use the following code snippet
      <?php foreach($page->parents()->append($page) as $parent) { echo "<li><a href='{$parent->url_redirect|url}'>{$parent->title}</a></li>"; } ?> Now to the problem: In my first code snippet above the
      url_redirect|url 
      works just fine but when I try something similiar in the second code snippet
      $parent->url_redirect|url
      I produce an server error How do I have to change the second code snippet so that it works in the correct way as the first code snippet does?
    • By Orkun
      Hi Guys
      How can I make a redirect inside the .htaccess to my custom maintenance.html file when any URL of my Website is accessed except the processwire admin (www.example.com/processwire/).
      Because I want that my User's still can access the website when they are loggedin in Processwire.
      When the current url starts with /processwire or if there is a processwire-login-cookie (Is there a cookie when user is logged in Processwire?) available the redirect should not work. Otherwise it should work.
      How can I achieve this?
    • By anderson
      Hi,
      Please take a look at this:
      https://templatemag.com/demo/Good/
      The upper nav bar, including dropdowns like "pages" and "portfolios", what do you call this whole thing? At first I guess it's called "dropdown nav bar", but seems not.
      AND of course, what's the simplest way/module to achieve this in PW?
      Thanks in advance.
×
×
  • Create New...