ProcessWire - Navigation Snippets

Aus Wikizone
Wechseln zu: Navigation, Suche

Basics

MarkupSimpleNavigation

Processwire Modul - MarkupSimpleNavigation

Shortcut und Platzhalter

Anpassung der renderNavTree() Funktion von Ryan Cramer. Options Field Installieren und als globales Feld "navigation_type" mit den Optionen

1=normal|Normal
2=doNotLink|Do not link
3=linkToFirstChild|Link to first childpage

konfigurieren. Dann Funktion etwa so anpassen:

...
// cycle through all the items
	foreach($items as $item) {

		// markup for the list item...
		// if current item is the same as the page being viewed, add a "current" class to it
		$out .= $item->id == wire('page')->id ? "<li class='current'>" : "<li>";

		// markup for the link
		$navigation_type = $item->navigation_type->id;
		switch ( $navigation_type ) {
			case 2: // do not link
				$out .= "<span class='spacer'>$item->title</span>";
				break;
			case 3: // link to subpage
				if( $item->hasChildren() ){
					$out .= '<a href="'.$item->child->url.'">'.$item->title.'</a>';
				}else{
					$out .= "<span class='spacer'>$item->title</span>";
				}
				break;
			default:
				$out .= "<a href='$item->url'>$item->title</a>";
				break;
		}
...

Redirect (301)

Redirect zu erster Unterseite

https://processwire.com/talk/topic/15-how-do-i-create-a-page-that-redirects-to-its-first-child/

Field redirects_to_first_child erstellen und im Template einfügen.

<?php 
  if($page->numChildren && $page->redirects_to_first_child) 
  $session->redirect($page->child()->url); 
?>

Erzeugt 301 Weiterleitung

Anderer Ansatz wäre evtl. den Link bei der Menügenerierung direkt zu generieren.

Redirect anderer Seite in der Navigation

https://processwire.com/talk/topic/762-howto-menu-item-that-links-to-another-page/

1. Create a new field and call it 'redirect_url' or something like that, and use the 'URL' fieldtype.

2. Add that field to your template where you'd want to use it, or create a new template just for the purpose, like a template named 'redirect'.

3. Edit the page that you want to be a symlink and populate the 'redirect_url' field with the URL you want it to redirect to.

4. In your nav-generation code that links to the pages, do something like this:

<?php
$url = $subpage->get("redirect_url|url"); // use redirect_url if there, otherwise use url
echo "<a href='$url'>{$subpage->title}</a>";

5. You might also want to add this to your template that has the 'redirect_url' field: just in case there's anything linking to it directly. That way it'll send people to the right place either way:

<?php
if($page->redirect_url) $session->redirect($page->redirect_url); 

Breadcrumb

	<!-- breadcrumbs -->
	<div class='breadcrumbs'><?php 
		// breadcrumbs are the current page's parents
		foreach($page->parents() as $item) {
			echo "<span><a href='$item->url'>$item->title</a></span> "; 
		}
		// optionally output the current page as the last item
		echo "<span>$page->title</span> "; 
	?></div>

Main Level Navigation Bar

Homepage + Kindseiten (1 Level). Die Variable $homepage wird i.d.R. in init.php definiert:

$homepage = $pages->get('/');
	<ul class='topnav'><?php 
		// top navigation consists of homepage and its visible children
		foreach($homepage->and($homepage->children) as $item) {
			if($item->id == $page->rootParent->id) {
				echo "<li class='current'>";
			} else {
				echo "<li>";
			}
			echo "<a href='$item->url'>$item->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'>Edit</a></li>";
	?></ul>

Nächste Seite

// add next button if there is another page next

if($page->next->id){
  $next = '<div class="next"><i>Next:</i>&nbsp;<a class="ajax-link" name="'.$page->next->title.'" href="'. $page->next->url .'">'. $page->next->title .'<span class="fa fa-arrow-right"><span></a></div>';
}

Kindseiten mit $page->children

<?=$page->children?>

Output

5723,4958,5937

Beispiel

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

Ergebnis:

<ul>

<li><a href='/about/contact/'>Contact Us</a></li>
<li><a href='/about/press/'>Press Releases</a></li>
<li><a href='/about/staff/'>Our Staff</a></li>

</ul>

Children Tree

function listChildrenTree($children, $current, $w) {

echo "<ul>";

foreach($children as $page) {

	$class = '';
	if($page === $current || $current->parents->slice(1)->has($page) ) {
		$class = "class='on' style='font-weight:bold'";
	}

	$rootid = $w->pages->get("/")->id;

	echo "<li><a href='{$page->url}' $class>";
	if($page->id == $rootid) echo "<img src='" . $w->config->urls->templates . "styles/images/home.png' width='24' height='28' alt='' />";
	echo "{$page->title}</a> ";

	if($page->numChildren && $page->id != $rootid) listChildrenTree($page->children, $current, $w);

	echo "</li>";
}
echo "</ul>";
}

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

listChildrenTree($children, $page, $wire);