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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By jploch
      Hey folks,
      currently Iam working on a website for one of my clients and I need some advice on how to approach this in PW.
      The website is for a company, that offers holiday houses in two locations. 

      The client wants the homepage to show the first location. Normally I just have a home template for the first page, but here the URL should reflect that you are in Location 1. So when you visit the URL casamani.com it should redirect to casamani.com/location-1. Not sure if this makes sense at all.

      Whould it be bad for SEO and performance reasons to redirect home to the Location-1 page?
      Another approach would be to render the Location-1 template on the home template or do an include like discussed here.
      Here is how the tree looks:
      – Home
      – Location 1 (Homepage)
           – Creation
           – Adventure
           – Sustainability
      – Location 2 
           – Creation
           – Adventure
           – Sustainability

      Thanks for looking into this!
    • By theoretic
      Hi guys and ladies! And thanks for Processwire!
      It appears i've got an interesting issue concerning the template-settings-based PW redirects dealing with access control. Any PW template has some access control options i.e. "Login redirect URL or page ID to render". If this option is used for a page having a template with this option filled, a redirect will occur if user is not logged in and/or has insufficient access rights.
      I like to hook PW events. In one of my current projects i decided to write an addHookBefore('Session::redirect', ...) which should store the page we are being redirected from. With "regular" redirects like $session->redirect('/somewhere/') this hook works like a charm. But it was strange to see that it doesn't work with the template-settings-based redirect.
      I'm too dumb to dive deep inside PW and to examine the whole PW session mechanism. But it could be rather logical if ANY redirect ( no matter template-settings-based or using $session->redirect() ) could be hooked in the same manner.
      Okay okay i can forget about template-settings-based redirect and write my own. Just a couple of lines of code, and it works. But it's less elegant than hooking the template-settings-based redirects.
      So am i missing something? It this behavior a bug, or is it intended by PW team? Thanks in advance for any comment!
    • By louisstephens
      I have not really had to make a site from the ground up in quite a bit as I have been mainly focused on internal apps using processwire. However, I have now been tasked with creating a site and I was quite excited. I started down the path of using bulma as it seemed like a good fit. I got the whole site coded statically and was not moving on to porting it over to processwire. I pretty much have the the whole site figured out save for tackling the navbar. My current navbar is set up as so:
      <section id="nav"> <nav class="navbar" role="navigation" aria-label="main navigation"> <div class="navbar-brand"> <a class="navbar-item is-size-3 has-text-primary" href="#">Logo goes here</a> <a role="button" class="navbar-burger burger" aria-label="menu" aria-expanded="false" data-target="navbarBasicExample"> <span aria-hidden="true"></span> <span aria-hidden="true"></span> <span aria-hidden="true"></span> </a> </div> <div id="navbarBasicExample" class="navbar-menu"> <div class="navbar-end"> <a class="navbar-item">Home</a> <div class="navbar-item has-dropdown is-hoverable"> <a class="navbar-link">Services</a> <div class="navbar-dropdown"> <a class="navbar-item">Subpage</a> <a class="navbar-item">Subpage</a> <a class="navbar-item">Subpage</a> <a class="navbar-item">Subpage</a> </div> </div> <a class="navbar-item">Gallery</a> <a class="navbar-item">Contact Us</a> </div> </div> </nav> </section> I guess my question is how would you go about setting this up in processwire? All pages will obviously be subpages of Home with the following structure
      Home
      --Services
      ----Subpage
      ----Subpage
      ----Subpage
      ----Subpage
      --Gallery
      --Contact Us
       
    • 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.
×
×
  • Create New...