Jump to content

How to include Home into the navigation - Blue VR Site Profile


L. Leopold
 Share

Recommended Posts

Hi guys,

I got a problem that drives me crazy. I just want to have the root of my page shown in the top navigation, together with its children. I've been searching for days by now, please help me.

in the navigation.inc file it says

Credits:
 *  I have used some same navigation methods used by Ryan Cramer.
 *  So full credit to Ryan Cramer
 */

so I compared Ryan Cramers navigation with the one used in the Blue VR Site. Cause in Ryans Foundation Site navigation the root (or Home) is already part of the navigation. It didn't help me much, I still can't figure out what to change.

https://github.com/ryancramerdesign/FoundationSiteProfile/blob/master/site-foundation/templates/_nav.php

https://github.com/gayanvirajith/BlueVrSiteProfile/blob/master/templates/_navigation.inc

I tried a lot of different things, that either didn't show any effect, or broke the complete site.

If you have a suggestion what could work, I'm gratefull for any hint!

Thanks in advance!

Link to comment
Share on other sites

There are many different ways to do this, so without seeing your code, it's hard to know what would work best to match what you have, but take a look at this:

https://github.com/ryancramerdesign/ProcessWire/blob/cffb682836517065d7dd7acf187545a4a80f1769/site-beginner/templates/_head.php#L21

See how the homepage is prepended to $children so that when it is foreach'd it will be included.

Does that help?

Link to comment
Share on other sites

I have tried that way already in different ways. I didn't find the right terms or the right place for it.

This is the current code for my navigation:


/**
 * Render a <ul> navigation list
 *
 */
function renderNav(PageArray $items, array $options = array()) {

    if(!count($items)) return '';

    $defaults = array(
        'class' => '', // class for the <ul>
        'active' => 'active', // class for active item
        'tree' => false, // render tree to reach current $page?
    );

    $options = array_merge($defaults, $options);
    $page = wire('page');
    $out = "<ul class='$options[class]'>";

    foreach($items as $item) {

        // if this item is the current page, give it an 'active' class
        $class = $item->id == $page->id ? " class='$options[active]'" : "";

        // render linked item title
        $out .= "<li$class><a href='$item->url'>$item->title</a> ";

        // optionally render a tree recursively to current $page
        if($options['tree']) {
            if($page->parents->has($item) || $item->id == $page->id) {
                $out .= renderNav($item->children("limit=50"), array(
                    'fields' => $options['fields'],
                    'tree' => true,
                ));
            }
        }

        $out .= "</li>";
    }

    $out .= "</ul>";
    return $out;
}

/**
 * Render a <ul> navigation list
 *
 */
function renderTopNav(PageArray $items, array $options = array(), $level = 0) {

    $defaults = array(
        'tree' => false, // number of levels it should recurse into the tree
        'dividers' => false,
        'repeat' => true, // whether to repeat items with children as first item in their children nav
    );

    $options = array_merge($defaults, $options);
    $divider = $options['dividers'] ? "<li class='divider'></li>" : "";

    $page = wire('page');
    $out = '';

    foreach($items as $item) {

        $numChildren = $item->numChildren(true);
        if($level+1 > $options['tree'] || $item->id == 1) $numChildren = 0;
        $class = '';
        $anchorProperties = "";
        $anchorClass = '';
        $dropdownCaret = '';

        $title = $item->title;

        if($numChildren) {
            $class .= "dropdown ";
            $anchorProperties = "data-toggle='dropdown'";
            $anchorClass = 'dropdown-toggle';
            $dropdownCaret = '<b class="caret"></b>';
        }
        if($page->id == $item->id) $class .= "current ";
        if(($item->id > 1 && $page->parents->has($item)) || $page->id == $item->id) $class .= "active ";
        if($class) $class = " class='" . trim($class) . "'";

        $out .= "$divider<li$class><a class='{$anchorClass}' href='$item->url' {$anchorProperties} title='{$title}'>$title $dropdownCaret</a>";

        if($numChildren) {
            $out .= "<ul class='dropdown-menu'>";
            if($options['repeat']) $out .= "$divider<li><a href='$item->url'>$item->title</a></li>";
            $out .= renderTopNav($item->children, $options, $level+1);
            $out .= "</ul>";
        }

        $out .= "</li>";
    }

    return $out;
}

Link to comment
Share on other sites

Ok, so in the Blue VR Site Profile take a look here:

https://github.com/gayanvirajith/BlueVrSiteProfile/blob/master/templates/_done.php#L84

This is where menu functions are being called. Try replacing that one line with this:

$homepage = $pages->get('/'); 
$children = $homepage->children();
// make 'home' the first item in the navigation
$children->prepend($homepage);
echo renderTopNav($children);

Untested, but I think that is what you need in that situation.

  • Like 2
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

×
×
  • Create New...