Jump to content

Prepending 'Home' instead of home page title to navigation?


antknight
 Share

Recommended Posts

I am using the nav code snippet from the default site, but I would like to display 'Home' in the nav instead of the home page title.

<ul id='topnav'><?php

   $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>";
   }
  ?></ul>

Any help much appreciated

Link to comment
Share on other sites

<ul id='topnav'><?php

$homepage = $pages->get("/");
$children = $homepage->children;
// we don't prepend the homepage to the array anymore

foreach($children as $child) {
   $class = $child === $page->rootParent ? " class='on'" : '';
   echo "<li><a$class href='{$homepage->url}'>Home</a></li>"; //echo the homepage
   echo "<li><a$class href='{$child->url}'>{$child->title}</a></li>";
}
?></ul>
Link to comment
Share on other sites

Well you could have a title field and a headline (like on basic profile)

You could name the title on home "Home" which will be taken for the navigation, and the headline your other title you output on the page. That's much better than hardcoding it.

  • Like 1
Link to comment
Share on other sites

oh, of course, sorry! the homepage has to go outside the loop

<ul id='topnav'><?php

$homepage = $pages->get("/");
$children = $homepage->children;
$class = $homepage === $page ? " class='on'" : '';
echo "<li><a$class href='{$homepage->url}'>Home</a></li>";

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

This should work, i also had to check for the class another time for the homepage only. This is not the DRYest code, but should work.

Try Soma's suggestion, seems more clever than this

Link to comment
Share on other sites

You can also take a different approach to soma's suggestion. Instead of changing title and headline, just create a new field "nav" for each page and change this in your code:

<ul id='topnav'><?php

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

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

Now you can choose the nav text for any page, and if left blank, the title will be used

  • 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.
×
×
  • Create New...