Jump to content

Page as "Home" navigation setup?


photoman355
 Share

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?  

Link to comment
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
Link to comment
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?
Link to comment
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 :)

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

Link to comment
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.

Link to comment
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
Link to comment
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
Link to comment
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
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...