antknight Posted December 1, 2012 Share Posted December 1, 2012 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 More sharing options...
diogo Posted December 1, 2012 Share Posted December 1, 2012 <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 More sharing options...
Soma Posted December 1, 2012 Share Posted December 1, 2012 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. 1 Link to comment Share on other sites More sharing options...
antknight Posted December 1, 2012 Author Share Posted December 1, 2012 Thank diogo but that doesn't work because it adds a home link foreach child page. I will take Soma's advice Link to comment Share on other sites More sharing options...
diogo Posted December 1, 2012 Share Posted December 1, 2012 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 More sharing options...
diogo Posted December 1, 2012 Share Posted December 1, 2012 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 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now