Jump to content

link to anchor in menu


Marcel Epp
 Share

Recommended Posts

Hi,

i need some help with the navigation menu. How can i add a link to an anchor?

I used this to display my sites. The menu renders fine. After the second entry i would link to screen2. How can i do that?

<?php
include("./head.php"); ?>

<body>

<section id="screen1">text

	<nav>
		<ul>
			<?php 
          // top navigation consists of homepage and its visible children
          $homepage = $pages->get('/'); 
          $children = $homepage->children();
          // make 'home' the first item in the navigation
          $children->prepend($homepage); 
          // render an <li> for each top navigation item
          foreach($children as $child) {
          if($child->id == $page->rootParent->id) {
          // this $child page is currently being viewed (or one of it's children/descendents)
          // so we highlight it as the current page in the navigation
          echo "<li class='current'><a href='$child->url'>$child->title</a></li>";
          } else {
          echo "<li><a href='$child->url'>$child->title</a></li>";}}
          // output an "Edit" link if this page happens to be editable by the current user
          if($page->editable()) {
          echo "<li class='edit'><a href='$page->editUrl'>Seite bearbeiten</a></li>"; }?>
		</ul>
	</nav>

</section>
	
<section id="screen2"></section>

<?php include("./footer.php"); ?>
Link to comment
Share on other sites

<?php 
          // top navigation consists of homepage and its visible children
          $homepage = $pages->get('/'); 
          $children = $homepage->children();
          // make 'home' the first item in the navigation
          $children->prepend($homepage); 
          $count_children = 0;

          // render an <li> for each top navigation item
          foreach($children as $child) {

          //change the css class if child is current
          $class= ($count_children == 0) ? " current " : "";

          //count entries
          $count_children++;

          //check if we got the second/third or something else item
          if ($count_entries == 1) 

                //counting starts by 0 so second item is 1! echo custom link
                echo "<li class='$class'><a href='#screen2'>My custom title</a></li>";

          } else {

          echo "<li class='$class'><a href='$child->url'>$child->title</a></li>";}

          }
?>

just written in the browser - may need some testing...use counting items to add some items between.

Changed the usage of the current/active class to some easier function to make it a little more DRY...

regards mr-fan

Link to comment
Share on other sites

So my errors show me that i've forgot one brace and set one wrong var name...and changed the condition to check the class on current...

so this have to work as expected.

Have you debug mode on in config.php? Get you PHP errors?

Here is the right example:

// top navigation consists of homepage and its visible children
$homepage = $pages->get('/');
$children = $homepage->children();

// make 'home' the first item in the navigation
$children->prepend($homepage);
$count_children = 0;

// render an <li> for each top navigation item
foreach($children as $child) {
    
//change the css class if child is current
$class= ($child === $page->rootParent) ? " current " : "";

//count entries
$count_children++;

//check if we got the second/third or something else item
if ($count_children == 1) {

    //counting starts by 0 so second item is 1! echo custom link
    echo '<li class="'.$class.'"><a href="'.$homepage->url.'#screen2">My custom title</a></li>';

} else {

    //normal menu entries
    echo '<li class="'.$class.'"><a href="'.$child->url.'">'.$child->title.'</a></li>';}

}

best regards mr-fan

  • 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...