Jump to content

Recommended Posts

Posted

Hi,

after a long time i use processwire again. But i have a problem and can't get a solution. I have build a navigation. I have to reuse this part. So i thought, i could write a function. I have searched the forum and i learned that i have to use wire() to get access to the sites inside the function. My problem is now, that everytime i click on a new site (children of home), the menu only shows the current page and there children. I know, that's what i have coded. ? Without the function i could use $home to get the root site and the children. How can i acess always the root page in the function? And display all the sites? I tried to get wire("pages") instead, but i don't know how tu use the object. 

I would be really thankful if someone shows me the right direction.

<?php 
namespace ProcessWire;
function navigation() {
  $page = wire("page");
  echo "<ul class='navbar-nav my-3 my-lg-0 fw-semibold rounded'>";
                
  foreach ($page->and($page->children()) as $item) {
    // set the active class 
    $active_link="";
    if ($page->id == $item->id) {
      $active_link="nav-link active p-3";
    } else {
      $active_link="nav-link p-3";
    }
    echo "<li class='nav-item'><a href='". $item->url ."' class='" . $active_link . "'>" . $item->title . "</a><li>";
  }
  echo "</ul>";
}
?>

 

Posted

If your function is placed in ready.php or init.php, then you might have to use wire('page') to get the current page. Have you tried simply using the reserved $page variable to see what would happen?

You can always use $pages->get(1) to get the default landing/homepage (or in this case, with the wire method, wire('pages')->get(1)). If the children of the standard ProcessWire homepage are your individual site's homepages, then break that logic out a bit more:
 

<?php 
foreach wire('pages')->get(1)->children as $homepage) {
    if (!wire('page') == $homepage) {
        continue; // Skip to the next iteration of the loop
    } else {
        // Place your navigation generating code here, except if you use this, change $page in your code above to $homepage
    }
}

 

Posted

Hi BrendonKoz,

thank you for your answer!

I came up with a solution by myself. But i will use your solution! Your code is smaller than mine! ?

My solution was also that i changed wire('page') to wire('pages'). Then i choosed the page->get()

<?php 
namespace ProcessWire;
function navigationold() {
  $pages = wire("pages");
  $page = wire("page");
  echo "<ul class='navbar-nav my-3 my-lg-0 fw-semibold rounded'>";

  $sites = $pages->get('home')->children();
  foreach ($sites as $item) {
    // set the active class 
    $active_link="";
    if ($page->id == $item->id) {
      $active_link="nav-link active p-3";
    } else {
      $active_link="nav-link p-3";
    }
    echo "<li class='nav-item'><a href='". $item->url ."' class='" . $active_link . "'>" . $item->title . "</a><li>";
  }
  echo "</ul>";
}
?>

<?php navigationold(); ?>

But then i tried to use your code and changed thinks for me:

<?php function navigation() {
  echo "<ul class='navbar-nav my-3 my-lg-0 fw-semibold rounded'>";
  foreach (wire('pages')->get(1)->children as $homepage) {
    if (!wire('page') == $homepage) {
      continue; // Skip to the next iteration of the loop
    } else {
      // Place your navigation generating code here, except if you use this, change $page in your code above to $homepage
      echo "<li class='nav-item'><a href='". $homepage->url ."' class='nav-link p-3";
      if (wire('page')->id == $homepage->id) {echo " active";}
      echo "'>". $homepage->title ."</a></li>";
    }
  }
  echo "</ul>";
}

?>
<?php navigation(); ?>

This looks really good so far. Next step for me is to show a homepage link to. Thank you! ?

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