ProcessWire - Snippets: Unterschied zwischen den Versionen

Aus Wikizone
Wechseln zu: Navigation, Suche
 
(49 dazwischenliegende Versionen von 12 Benutzern werden nicht angezeigt)
Zeile 1: Zeile 1:
 +
Hier findest du ein paar Basis Snippets. Codebeispiele zu Feldern findest du z.B. über die Seiten zu den Field Types.
 +
 +
== Siehe auch ==
 +
[[ProcessWire - Module Snippets]]
 +
[[ProcessWire - Navigation Snippets]]
 +
[[ProcessWire - Debugging Snippets]]
 +
[[ProcessWire - Selectors]]
 +
 
== Basics ==
 
== Basics ==
 
=== Bild in Template einbinden ===
 
=== Bild in Template einbinden ===
Zeile 8: Zeile 16:
 
  <div class="<? echo $page->get("content_class")?>">
 
  <div class="<? echo $page->get("content_class")?>">
 
== Bilder ==
 
== Bilder ==
 +
 +
=== Umfassende Renderfunktion zum wiederverwenden ===
 +
Hier immer wieder aktualisiert. Todo Option für srcset.
 +
<syntaxhighlight lang="php">
 +
 +
/**
 +
* render image tag from imageobject
 +
* @todo add srcset version,add ukimage version, other attributes
 +
* @todo attributes as object or string
 +
* @param Object Image
 +
* @param Int width
 +
* @return String
 +
*/
 +
function renderImage($image,$w=200,$options=array()){
 +
  if(!$image instanceof \ProcessWire\Pageimage) return '';
 +
  $defaults = array(
 +
    'resizeHeight' => false, // use height instead of width to resize
 +
    'srcset' => false, // todo
 +
    'uikit' => false, // todo
 +
    'aspect' => false,
 +
    'noResize' => false,
 +
    'classes' => '', // classes as string
 +
'attributes' => false, // attributes as array(attr=>val) or string
 +
'title' => true, // add title tag based on image description
 +
'alt' => true, // add alt tag based on image description
 +
  );
 +
  $options = array_merge($defaults,$options);
 +
$attributes = array();
 +
$strAttr = '';
 +
  $classMarkup = '';
 +
 +
// ATTRIBUTES
 +
  $attributesMarkup = '';
 +
if( $options['attributes'] ){
 +
bd( gettype( $options['attributes'] ) );
 +
if( gettype($options['attributes'] ) == 'string') $strAttr = (string)$options['attributes'];
 +
else if( gettype($options['attributes']) == 'array' ){
 +
foreach( (array)$options['attributes'] as $k => $a ){
 +
$attributes[$k] = $a;
 +
}
 +
}
 +
}
 +
if($options['alt'] && $image->description) $attributes['alt'] = $image->description;
 +
if($options['title'] && $image->description) $attributes['title'] = $image->description;
 +
$attributesMarkup = implode(' ', $attributes);
 +
if($strAttr) $attributesMarkup .= " $strAttr";
 +
 +
  if($options['classes']) $classMarkup = ' class="'.$options['classes'].'"';
 +
 +
if($options['resizeHeight']){
 +
    $i = $image->height($w);
 +
  }else if($options['aspect']){
 +
    $i = $image->size($w, intval( $w / $options['aspect']) );
 +
  }else if($options['noResize']){
 +
    // Todo
 +
 +
  }else{
 +
    $i = $image->width($w);
 +
  }
 +
$out = '<img src="'.$i->url.'" '.$classMarkup.$attributesMarkup.'>';
 +
return $out;
 +
}
 +
</syntaxhighlight>
 +
 +
=== Advanced Image Manipulation ===
 +
ImageMagick, Externe Bilder laden, Bilder Vorrendern...
 +
[[Processwire - Advanced Image Manipulation]]
 +
 +
=== Praktische Renderfunktionen ===
 +
<syntaxhighlight lang="php">
 +
/**
 +
* Image Function
 +
*/
 +
 +
/**
 +
* get background-position style via focus point if available
 +
* mainly used in other functions
 +
* @param Pageimage PW image object
 +
* @return Array containing background-position
 +
*/
 +
function getFocusStyles($image){
 +
// Focal Points if available
 +
$imageStyles = array();
 +
if($image->focus){
 +
$focus=$image->focus();
 +
//var_dump($focus);
 +
$imageStyles[] = 'background-position:'.number_format($focus["left"],1,".","") .'% '.number_format($focus["top"],1,".","").'%;';
 +
}
 +
return $imageStyles;
 +
}
 +
 +
/**
 +
* get background-image styles and create image
 +
* @todo - make image set instead of single image
 +
* @param Pageimage
 +
* @param Int Image width
 +
* @param String background-size attribute (cover, contain...)
 +
* @return array
 +
*/
 +
function getBgImageStyles($image, $w = 800,$options = array()){
 +
  $defaults = array(
 +
    'size' => 'cover',
 +
    'noResize' => false
 +
  );
 +
  $options = array_merge($defaults,$options);
 +
 
 +
if($image){
 +
$styles = array();
 +
    if ($options['noResize']) $myImage = $image;
 +
    else $myImage = $image->width($w);
 +
$styles[] = "background-image: url($myImage->url);";
 +
$styles[] = 'background-size: '.$options['size'].'; background-repeat: no-repeat;';
 +
$styles = array_merge( $styles, getFocusStyles($myImage) );
 +
return $styles;
 +
} else return array();
 +
 +
}
 +
 +
/**
 +
* render image tag from imageobject
 +
* @todo add srcset version, add description, add ukimage version, other attributes
 +
* @param Object Image
 +
* @param Int width
 +
* @return String
 +
*/
 +
function renderImage($image,$w=200,$options=array()){
 +
  if(!$image instanceof \ProcessWire\Pageimage) return '';
 +
  $defaults = array(
 +
    'resizeHeight' => false, // use height instead of width to resize
 +
    'srcset' => false, // todo
 +
    'uikit' => false, // todo
 +
    'aspect' => false,
 +
    'noResize' => false
 +
  );
 +
  $options = array_merge($defaults,$options);
 +
if($options['resizeHeight']){
 +
    $i = $image->height($w);
 +
  }else if($options['aspect']){
 +
    $i = $image->size($w, intval( $w / $options['aspect']) );
 +
  }else if($options['noResize']){
 +
    // Todo
 +
 
 +
  }else{
 +
    $i = $image->width($w);
 +
  }
 +
$out = '<img src="'.$i->url.'" alt="'.$i->description.'">';
 +
return $out;
 +
}
 +
</php>
 +
</syntaxhighlight>
 +
 
=== Einzelbild einfügen ===
 
=== Einzelbild einfügen ===
 +
 +
==== Maximale Breite oder maximale Höhe je nach Orientierung ====
 +
$thumburl = $image->width > $image->height ? $image->size(450,0)->url : $image->size(0,320);
 +
 +
==== Maximale Breite und maximale Höhe berücksichtigen ====
 +
<syntaxhighlight lang="php">
 +
$mW = 180;
 +
$mH = 80;
 +
$thumbUrl = ($mH / $image->height) > ($mW / $image->width) ? $image->size($mW,0)->url : $image->size(0,$mH)->url;
 +
</syntaxhighlight>
 +
 
====Bild aus Backend als Hintergrund einfügen====
 
====Bild aus Backend als Hintergrund einfügen====
 
(Feldname hier: ''main_image'')
 
(Feldname hier: ''main_image'')
Zeile 24: Zeile 194:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
==== n-tes Bild einfügen ===
+
=== n-tes Bild einfügen ===
 
  $page->images->eq(1); // second image
 
  $page->images->eq(1); // second image
  
Zeile 44: Zeile 214:
 
}, array('prepend' => '<ul>', 'append' => '</ul>'));   
 
}, array('prepend' => '<ul>', 'append' => '</ul>'));   
 
</syntaxhighlight>
 
</syntaxhighlight>
==== Einfache Gallerie ====
+
 
 +
==== Einfache Galerie ====
 
<syntaxhighlight lang="php">
 
<syntaxhighlight lang="php">
 
$gallery = '<div class="gallery">';
 
$gallery = '<div class="gallery">';
Zeile 60: Zeile 231:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
== Navigation ==
 
=== 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.
 
<syntaxhighlight lang="php">
 
<?php
 
  if($page->numChildren && $page->redirects_to_first_child)
 
  $session->redirect($page->child()->url);
 
?>
 
</syntaxhighlight>
 
Erzeugt 301 Weiterleitung
 
 
Anderer Ansatz wäre evtl. den Link bei der Menügenerierung direkt zu generieren.
 
  
=== Redirect anderer Seite in der Navigation ===
+
=== Slider mit Processwire ===
https://processwire.com/talk/topic/762-howto-menu-item-that-links-to-another-page/
+
[[Processwire - Slider]]
  
1. Create a new field and call it 'redirect_url' or something like that, and use the 'URL' fieldtype.
+
== Videos ==
 +
[[Processwire - Working with Video]]
  
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'.
+
== File Uploads ==
 +
[[ProcessWire - Uploads]]
  
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.
+
== Navigation ==
 
+
[[ProcessWire - Navigation Snippets]]
4. In your nav-generation code that links to the pages, do something like this:
 
<syntaxhighlight lang="php">
 
<?php
 
$url = $subpage->get("redirect_url|url"); // use redirect_url if there, otherwise use url
 
echo "<a href='$url'>{$subpage->title}</a>";
 
</syntaxhighlight>
 
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 ===
 
<pre>
 
<!-- 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>
 
</pre>
 
 
 
=== Navigation Bar ===
 
Homepage + Kindseiten (1 Level). Die Variable $homepage wird i.d.R. in init.php definiert:
 
$homepage = $pages->get('/');
 
<pre>
 
<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>
 
</pre>
 
 
 
=== Nächste Seite ===
 
// add next button if there is another page next
 
<syntaxhighlight lang="php">
 
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>';
 
}
 
</syntaxhighlight>
 
 
 
=== Kindseiten mit $page->children ===
 
<?=$page->children?>
 
Output
 
5723,4958,5937
 
 
 
Beispiel
 
<syntaxhighlight lang="php">
 
<ul>
 
<?php
 
foreach($page->children as $child)
 
  echo "<li><a href='{$child->url}'>{$child->title}</a></li>";
 
?>

 
</ul>
 
</syntaxhighlight>
 
Ergebnis:
 
<pre>
 
<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>
 
</pre>
 
  
 
== Seiten ==
 
== Seiten ==
Zeile 172: Zeile 258:
 
<p>Visit us at: <?=$pages->get("/about/contact/")->address?></p>
 
<p>Visit us at: <?=$pages->get("/about/contact/")->address?></p>
 
</syntaxhighlight>
 
</syntaxhighlight>
=== Mehrere Seiten finden mit find() ===
+
 
"Featured" Checkbox im Backend angehakt.
+
=== Seiten Filtern find() ===
 +
==== "Featured" Checkbox im Backend angehakt ====
 
<syntaxhighlight lang="php">
 
<syntaxhighlight lang="php">
 
<ul>
<?php

 
<ul>
<?php

Zeile 181: Zeile 268:
 
?>
</ul>
 
?>
</ul>
 
</syntaxhighlight>
 
</syntaxhighlight>
Komplexere Beispiele
 
  
Limitiert, sortiert und in Unterverzeichnis
+
==== Beispiel: Limitierte Ausgabe, Sortieren und in Unterverzeichnis ====
 
  $pages->find("parent=/about/press/, featured=1, limit=3, sort=-date");
 
  $pages->find("parent=/about/press/, featured=1, limit=3, sort=-date");
 
oder
 
oder
Zeile 189: Zeile 275:
 
Finde in Kategorie
 
Finde in Kategorie
 
  $pages->get("/about/press/")->find("featured=1, limit=3, sort=-date");
 
  $pages->get("/about/press/")->find("featured=1, limit=3, sort=-date");
Finde über Template
+
 
 +
==== Seiten mit Template xyz ====
 
  $pages->find("template=press_release, featured=1, limit=3, sort=-date");
 
  $pages->find("template=press_release, featured=1, limit=3, sort=-date");
 
Ausführliches Beispiel mit Ausgabe
 
Ausführliches Beispiel mit Ausgabe
Zeile 204: Zeile 291:
 
?></ul>
 
?></ul>
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 
== Felder ==
 
== Felder ==
 +
=== Label zu Feldes und Subfeldern ausgeben ===
 +
Beispiel mit dem Combo Profield. Das Prinzip funktioniert bei allen Feldern.
 +
https://processwire.com/talk/topic/26183-getting-label-of-combo-subfield/#comment-217749
 +
<syntaxhighlight lang="php">
 +
$field = $fields->get('your_combo_field_name');
 +
$settings = $field->getComboSettings();
 +
$subfield = $settings->getSubfield('your_subfield_name');
 +
 +
// get label in current user language
 +
$label = $subfield->getLabel();
 +
 +
// to get in a language other than current user language
 +
$label = $subfield->getLabel('de');
 +
 +
// to get localized description
 +
$description = $subfield->getDescription();
 +
 +
// to get localized notes
 +
$notes = $subfield->getNotes();
 +
 +
// also note you can use getLanguageValue
 +
$label = $subfield->getLanguageValue('de', 'label');
 +
</syntaxhighlight>
 +
 +
=== Label eines Felds ausgeben ===
 +
Geht über das Feld-Objekt.
 +
echo $fields->get("body")->label;
 +
 
=== Ist ein Feld leer ? ===
 
=== Ist ein Feld leer ? ===
 
  if($page->summary_de) { ... }
 
  if($page->summary_de) { ... }
 
  // check whitespace
 
  // check whitespace
 
  if(trim($page->summary_de)) { ... }
 
  if(trim($page->summary_de)) { ... }
 +
 +
=== Datumsfelder formatieren ===
 +
<syntaxhighlight lang="php">
 +
 +
// aus Datumsfeldern
 +
echo date("Y-m-d", $page->getUnformatted("closing"));
 +
 +
// aus Unix Timestamp mit Vanilla PHP
 +
echo date("Y-m-d", $news->created);
 +
 +
setlocale(LC_TIME, "de_DE.utf8");
 +
$weekday = strftime ( '%A' , $page->getUnformatted("date") );
 +
$date = date("d.m.Y", $page->getUnformatted("date"));
 +
</syntaxhighlight>
 +
 +
Beispiel aus ProcessWire Blog
 +
<syntaxhighlight lang="php">
 +
setlocale(LC_ALL, array('de_DE','de_DE.iso88591','de_DE.iso885915@euro','de_DE.utf8','de_DE@euro'));
 +
$date = $page->date ? date('d.m.Y',$page->getUnformatted("date")) : date('d.m.Y',$page->getUnformatted("createdStr"));
 +
</syntaxhighlight>
 +
 +
=== Optionen eines Select Felds ausgeben (fieldtypeOptions)===
 +
<syntaxhighlight lang="php">
 +
$options = $fieldtypes->get('FieldtypeOptions')->getOptions('YOURFIELDNAME');
 +
foreach($options as $option) {
 +
  echo $option->id;
 +
  echo $option->value;
 +
  echo $option->title;
 +
}
 +
</syntaxhighlight>
 +
 +
=== Parent von Repeater oder RepeaterMatrix finden ===
 +
Nutzt man ein Repeater Feld muss man manchmal die Seite herausfinden in der das Feld enthalten ist. Hierfür gibt es die Funktion getForPage().
 +
<syntaxhighlight lang="php">
 +
if( get_class($myPage) == 'ProcessWire\RepeaterMatrixPage' || get_class($myPage) == 'ProcessWire\RepeaterPage') {
 +
            $forPage = $myPage->getForPage();
 +
            //var_dump( $myPage->getForPage());
 +
            return $myPage = getImagePage($forPage);
 +
}
 +
</syntaxhighlight>
 +
 +
Falls man Repeater in Repeatern hat kann man auch eine rekursive Funktion nutzen:
 +
 +
<syntaxhighlight lang="php">
 +
if (!function_exists('getForPage')) {
 +
   
 +
  function getForPage($myPage, $level=1){
 +
      // if in repeater or repeaterMatrix find the first parent ProcessWire\Page Page
 +
      $maxLevel=5;
 +
      if( get_class($myPage) == 'ProcessWire\RepeaterMatrixPage' || get_class($myPage) == 'ProcessWire\RepeaterPage') {
 +
          $level +=1;
 +
          if($level > $maxLevel) return false;
 +
          $forPage = $myPage->getForPage();
 +
          //var_dump( $myPage->getForPage());
 +
          //echo("<p>next Level: ".get_class($forPage)."</p>");
 +
          return $myPage = getForPage($forPage);
 +
       
 +
      }else if( get_class($myPage) == 'ProcessWire\Page'){
 +
         
 +
          return $myPage;
 +
      }
 +
  }
 +
}
 +
</syntaxhighlight>
  
 
== Searchbar ==
 
== Searchbar ==
Zeile 219: Zeile 399:
  
 
</pre>
 
</pre>
== Editor Link ==
+
== Create Admin User ==
 +
=== Reset Password - Passwort zurücksetzen ===
 +
<syntaxhighlight lang="php">
 +
<?php
 +
require "index.php";
 +
$admin = $users->get('admin1'); // or whatever your username is
 +
$admin->setAndSave('pass', 'geheim');
 +
?>
 +
</syntaxhighlight>
 +
 
 +
https://processwire-recipes.com/recipes/resetting-admin-password-via-api/
 +
Problem
 +
For some reason, you have managed to lock yourself out of a site you are currently developing.
 +
 
 +
Solution
 +
Paste the following into a file (e.g. "reset.php") in the root folder of the site, then run it.
 +
 
 +
ProcessWire version >= 2.6.9
 +
 
 +
<pre>
 +
require "index.php";
 +
$admin = $users->get('admin'); // or whatever your username is
 +
$admin->setAndSave('pass', 'yo123456');
 +
</pre>
 +
 
 +
ProcessWire version < 2.6.9
 +
 
 +
<pre>
 +
require "index.php";
 +
$admin = wire('users')->get('admin');
 +
$admin->setOutputFormatting(false);
 +
$admin->set('pass', 'yo12345');
 +
$admin->save('pass');
 +
</pre>
 +
 
 +
== Editor Link / Login Logout Link ==
 
_main.php
 
_main.php
 
<syntaxhighlight lang="php">
 
<syntaxhighlight lang="php">
Zeile 236: Zeile 451:
 
</div>
 
</div>
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
Siehe auch [[ProcessWire - User Login / Logout]]
  
 
== Conditions ==
 
== Conditions ==
 +
=== Bedingungen durch Oder Operator in Feldern ===
 +
Wenn das eine Feld leer ist nimm das nächste...
 +
$t = $page->get("long_title|title");
 
=== Conditions für Navigation ===
 
=== Conditions für Navigation ===
 
<syntaxhighlight lang="php">
 
<syntaxhighlight lang="php">
Zeile 255: Zeile 474:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
== Rendering ==
 +
Es gibt viele Möglichkeiten deshalb ein eigenes Kapitel:
 +
[[ProcessWire - Render Funktionen]]
 +
 +
== $page Object ==
 +
Siehe auch [[ProcessWire - Page Object]]
 +
=== Zugriff auf $page in Funktionen ===
 +
Beispiel:
 +
<pre>
 +
function getTitle() {
 +
    $page = wire("page");
 +
    $t = $page->title;
 +
</pre>
 +
oder einfach übergeben.
 +
=== Variablen zu Render-Datei senden ===
 +
<pre>
 +
// you can send vars (here item count)
 +
// use this inside the field rendering script
 +
// ie. $page->_n
 +
$page->set('_n',$n);
 +
$page->set('_type',$item->type);
 +
$out .= $item->render();
 +
</pre>
 +
 
== Copy and Paste ==
 
== Copy and Paste ==
 +
=== Einfache Datumssteuerung ===
 +
Setzt Felder date_begin und date_end voraus.
 +
<syntaxhighlight lang="php">
 +
// Date check
 +
$dateValid = true;
 +
$now = time();
 +
$start = 0;
 +
$end = 0;
 +
if($page->date_begin){
 +
  $start = strtotime( $page->date_begin('Y-m-d') . " 00:00:00");
 +
  if( !($now >= $start) ) $dateValid = false;
 +
}
 +
if($page->date_end){
 +
  $end = strtotime( $page->date_end('Y-m-d') . " 23:59:59");
 +
  if( ($now > $end) ) $dateValid = false;
 +
}
 +
 +
if(!$dateValid) return '';
 +
</syntaxhighlight>
 +
 +
=== Copyright / Published Date ===
 +
<pre>
 +
$published_date = date('d.m.Y',$page->created);
 +
<?= '&copy;'.$published_date ?>
 +
</pre>
 +
 +
=== Stylesheets und Skripte mit AIOM ===
 +
<script src="<?php echo AIOM::JS(array('scripts/jquery-2.2.4.min.js','scripts/jquery.chocolat.js','scripts/jquery.fitvids.js','scripts/jquery.flexslider-min.js', 'scripts/main.js')); ?>"></script>
 +
<link rel="stylesheet" type="text/css" href="<?php echo AIOM::CSS('styles/main.less'); ?>">
 +
<?php echo $homepage->site_title .' '. Date('Y'); ?>
 +
 +
=== Link zur Seite anhand URL-Pfad ===
 
  <?php echo $pages->get('/ueber-uns/')->url; ?>
 
  <?php echo $pages->get('/ueber-uns/')->url; ?>
+
 
 +
=== Feld einer Seite ===
 
  $content_left = $page->get('content_left');
 
  $content_left = $page->get('content_left');
  
 +
=== Bild ===
 
  if($page->main_image){$image = $page->main_image->size(640,960);}
 
  if($page->main_image){$image = $page->main_image->size(640,960);}
 
  else $image = NULL;
 
  else $image = NULL;
 +
=== Bild von der Homepage ===
 +
in _init.php
 +
$homepage = $pages->get('/');
 +
im Template
 +
if($homepage->config_logo){$logo = $homepage->config_logo->width(280);}
 +
else $logo = NULL;
  
 +
=== Simple Navigation ===
 
<pre>
 
<pre>
 
<ul class='nav-list'>
 
<ul class='nav-list'>
<?php
+
  <?php
// Childrens
+
  // Childrens
foreach(($page->children) as $item) {
+
  foreach(($page->children) as $item) {
if($item->id == $page->rootParent->id) {
+
    if($item->id == $page->rootParent->id) {
echo '<li class="current">';
+
      echo '<li class="current">';
} else {
+
    } else {
echo '<li>';
+
      echo '<li>';
}
+
    }
echo '<a class="ajax-link" href="'.$item->url.'">'.$item->title.'</a></li>';
+
    echo '<a class="ajax-link" href="'.$item->url.'">'.$item->title.'</a></li>';
}
+
  }
?>
+
  ?>
</ul>
+
</ul>
 
</pre>
 
</pre>
  
 +
=== Auflistung mit div. Feldern aus Kindseiten ===
 
<syntaxhighlight lang="php">
 
<syntaxhighlight lang="php">
<?php
 
 
 
/**
 
/**
  * art list template  
+
  * Publications list template
 
  *
 
  *
 
  */
 
  */
 +
$publications = '';
 +
$children = $page->children;
 +
foreach($children as $child){
 +
  $publications .= '<div class="box">';
 +
  $publications .= '<h2><a class="ajax-link" parent="'. $child->parent->title .'" name="'.$child->title.'" href="'. $child->url. '">'. $child->title . '</a></h2>';
 +
  $publications .= $child->body;
 +
  $publications .= '</div>';
 +
}
 +
 +
$content = $page->body . $publications;
 +
</syntaxhighlight>
  
 +
=== Minigalerie ===
 +
Thumbnails / Child Pages
 +
<syntaxhighlight lang="php">
 
$works = '<div class="gallery">';
 
$works = '<div class="gallery">';
 
$children = $page->children;
 
$children = $page->children;
Zeile 300: Zeile 597:
 
}
 
}
 
$works .= '</div>';
 
$works .= '</div>';
 +
  
 
$content = $page->body . $works . '<br/>' . $page->video;
 
$content = $page->body . $works . '<br/>' . $page->video;
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
=== Page Navigation ===
 +
<syntaxhighlight lang="php">
 +
// 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>';
 +
}
 +
 +
$content = '<h2>'. $page->title . '</h2>' . $page->body . '<br/><div class="video">' . $page->video .'</div>' . $next;
 +
</syntaxhighlight>
 +
 +
=== Simple Gallery with next Page ===
 +
<syntaxhighlight lang="php">
 +
$gallery = '<div class="gallery">';
 +
$images = $page->gallery;
 +
 +
// add thumbnail to gallery
 +
if(count($page->thumbnail)){
 +
    $gallery .= '<a class="chocolat-image gallery-item" href="'.$page->thumbnail->url.'"><img src="'. $page->thumbnail->getThumb('thumbnail') .'" title="'. $image->description .'"></a>';
 +
}
 +
 +
foreach($images as $image){
 +
    $gallery .= '<a class="chocolat-image gallery-item" href="'.$image->url.'"><img src="'. $image->getThumb('thumbnail') .'" title="'. $image->description .'"></a> ';
 +
}
 +
$gallery .= '</div>';
 +
 +
// 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>';
 +
}
 +
 +
 +
$content = '<h2>'. $page->title . '</h2>' . $page->body . '<br/>' . $gallery . '<br/><div class="video">' . $page->video .'</div>'. $next;
 +
 +
</syntaxhighlight>
 +
 +
=== Platzhalter-Seite mit Redirect auf erste Unterseite ===
 +
Möchte man eine Eltern-Seite für das Menü generieren die keinen eigenen Inhalt besitzt sondern nur Unterseiten enthält, kann man ein Template für diese Seite erstellen, dass den User auf die erste Unterseite weiterleitet.
 +
 +
Diese Lösung ist ein regulärer Redirect. Ob das die Beste Lösung ist müßte man mal überlegen. Auf alle Fälle besser als Duplicate Content.
 +
<syntaxhighlight lang="php">
 +
<?php namespace ProcessWire;
 +
$root = $pages->get("/");
 +
if($page->numChildren){
 +
$session->redirect($page->child()->url);
 +
} else $session->redirect($root->url);
 +
?>
 +
 +
</syntaxhighlight>
 +
 +
=== In Funktionen auf Page Objekt zugreifen ===
 +
Entweder als Parameter übergeben (evtl. by reference) oder wenn ein ProcessWire Objekt in der Funktion vorliegt das vom Wire Objekt abgeleitet ist (fast alle) kannst du dieses nutzen:
 +
$page = $items->wire('page'); // $items is a PageArray - use its wire to get current page
 +
 +
== ProcessWire - Debugging Snippets ==
 +
[[ProcessWire - Debugging Snippets]]

Aktuelle Version vom 30. April 2025, 09:12 Uhr

Hier findest du ein paar Basis Snippets. Codebeispiele zu Feldern findest du z.B. über die Seiten zu den Field Types.

Siehe auch[Bearbeiten]

ProcessWire - Module Snippets
ProcessWire - Navigation Snippets
ProcessWire - Debugging Snippets
ProcessWire - Selectors

Basics[Bearbeiten]

Bild in Template einbinden[Bearbeiten]

<img src="<?php echo $config->urls->templates?>img/menu.png" alt="Home">

Link zu Seite[Bearbeiten]

<a href="<?php echo $pages->get('/kontakt/')->url; ?>">

Wert aus Textfeld[Bearbeiten]

<div class="<? echo $page->get("content_class")?>">

Bilder[Bearbeiten]

Umfassende Renderfunktion zum wiederverwenden[Bearbeiten]

Hier immer wieder aktualisiert. Todo Option für srcset.

/**
 * render image tag from imageobject
 * @todo add srcset version,add ukimage version, other attributes
 * @todo attributes as object or string
 * @param Object Image
 * @param Int width
 * @return String
 */
function renderImage($image,$w=200,$options=array()){
  if(!$image instanceof \ProcessWire\Pageimage) return '';
  $defaults = array(
    'resizeHeight' => false, // use height instead of width to resize
    'srcset' => false, // todo
    'uikit' => false, // todo
    'aspect' => false,
    'noResize' => false,
    'classes' => '', // classes as string
		'attributes' => false, // attributes as array(attr=>val) or string
		'title' => true, // add title tag based on image description
		'alt' => true, // add alt tag based on image description
  );
  $options = array_merge($defaults,$options);
	$attributes = array();
	$strAttr = '';
  $classMarkup = '';

	// ATTRIBUTES
  $attributesMarkup = '';
	if( $options['attributes'] ){
		bd( gettype( $options['attributes'] ) );
		if( gettype($options['attributes'] ) == 'string') $strAttr = (string)$options['attributes'];
		else if( gettype($options['attributes']) == 'array' ){
			foreach( (array)$options['attributes'] as $k => $a ){
				$attributes[$k] = $a;
			}
		}
	}
	if($options['alt'] && $image->description) $attributes['alt'] = $image->description;
	if($options['title'] && $image->description) $attributes['title'] = $image->description;
	$attributesMarkup = implode(' ', $attributes);
	if($strAttr) $attributesMarkup .= " $strAttr";
	
  if($options['classes']) $classMarkup = ' class="'.$options['classes'].'"';

	if($options['resizeHeight']){
    $i = $image->height($w);
  }else if($options['aspect']){
    $i = $image->size($w, intval( $w / $options['aspect']) );
  }else if($options['noResize']){
    // Todo

  }else{
    $i = $image->width($w);
  } 
	$out = '<img src="'.$i->url.'" '.$classMarkup.$attributesMarkup.'>';
	return $out;
}

Advanced Image Manipulation[Bearbeiten]

ImageMagick, Externe Bilder laden, Bilder Vorrendern... Processwire - Advanced Image Manipulation

Praktische Renderfunktionen[Bearbeiten]

/**
 * Image Function
 */

/**
 * get background-position style via focus point if available
 * mainly used in other functions
 * @param Pageimage PW image object
 * @return Array containing background-position
 */
function getFocusStyles($image){
	// Focal Points if available
	$imageStyles = array();
	if($image->focus){
		$focus=$image->focus();
		//var_dump($focus);
		$imageStyles[] = 'background-position:'.number_format($focus["left"],1,".","") .'% '.number_format($focus["top"],1,".","").'%;';
	}
	return $imageStyles;
}

/**
 * get background-image styles and create image
 * @todo - make image set instead of single image
 * @param Pageimage
 * @param Int Image width
 * @param String background-size attribute (cover, contain...)
 * @return array 
 */
function getBgImageStyles($image, $w = 800,$options = array()){
  $defaults = array(
    'size' => 'cover',
    'noResize' => false
  );
  $options = array_merge($defaults,$options);
  
	if($image){
		$styles = array();
    if ($options['noResize']) $myImage = $image;
    else $myImage = $image->width($w);
		$styles[] = "background-image: url($myImage->url);";
		$styles[] = 'background-size: '.$options['size'].'; background-repeat: no-repeat;';
		$styles = array_merge( $styles, getFocusStyles($myImage) );
		return $styles;
	} else return array();
	
}

/**
 * render image tag from imageobject
 * @todo add srcset version, add description, add ukimage version, other attributes
 * @param Object Image
 * @param Int width
 * @return String
 */
function renderImage($image,$w=200,$options=array()){
  if(!$image instanceof \ProcessWire\Pageimage) return '';
  $defaults = array(
    'resizeHeight' => false, // use height instead of width to resize
    'srcset' => false, // todo
    'uikit' => false, // todo
    'aspect' => false,
    'noResize' => false
  );
  $options = array_merge($defaults,$options);
	if($options['resizeHeight']){
    $i = $image->height($w);
  }else if($options['aspect']){
    $i = $image->size($w, intval( $w / $options['aspect']) );
  }else if($options['noResize']){
    // Todo
  
  }else{
    $i = $image->width($w);
  } 
	$out = '<img src="'.$i->url.'" alt="'.$i->description.'">';
	return $out;
}
</php>

Einzelbild einfügen[Bearbeiten]

Maximale Breite oder maximale Höhe je nach Orientierung[Bearbeiten]

$thumburl = $image->width > $image->height ? $image->size(450,0)->url : $image->size(0,320);

Maximale Breite und maximale Höhe berücksichtigen[Bearbeiten]

$mW = 180;
$mH = 80;
$thumbUrl = ($mH / $image->height) > ($mW / $image->width) ? $image->size($mW,0)->url : $image->size(0,$mH)->url;

Bild aus Backend als Hintergrund einfügen[Bearbeiten]

(Feldname hier: main_image)

if($page->main_image){$image = $page->main_image->size(640,960);}
else $image = NULL;
...
if($image){
  echo "
  <div class='a-3-4 team-image' style='background-image:url({$image->url})'>
    <div>{$myContent}</div>
  </div>
  ";
}

n-tes Bild einfügen[Bearbeiten]

$page->images->eq(1); // second image

Mehrere Bilder aus dem Backend[Bearbeiten]

$myImages='';
if(count($page->images)) {
  foreach($page->images as $image) {
    $myImages .=  '<img class="img-responsive" src="'.$image->url.'">';
  }
}

Beispiel 2

echo $page->images->implode(function($item) {
  $large = $item->size(1200,800);
  $thumb = $item->size(400,300);
  return "<li><a href='$large->url'><img src='$thumb->url' alt='$item->description'></a></li>";
}, array('prepend' => '<ul>', 'append' => '</ul>'));

Einfache Galerie[Bearbeiten]

$gallery = '<div class="gallery">';
$images = $page->gallery;

// add thumbnail to gallery
if(count($page->thumbnail)){
    $gallery .= '<a class="chocolat-image gallery-item" href="'.$page->thumbnail->url.'"><img src="'. $page->thumbnail->getThumb('thumbnail') .'" title="'. $image->description .'"></a>';
}

foreach($images as $image){
    $gallery .= '<a class="chocolat-image gallery-item" href="'.$image->url.'"><img src="'. $image->getThumb('thumbnail') .'" title="'. $image->description .'"></a> ';
}
$gallery .= '</div>';


Slider mit Processwire[Bearbeiten]

Processwire - Slider

Videos[Bearbeiten]

Processwire - Working with Video

File Uploads[Bearbeiten]

ProcessWire - Uploads

Navigation[Bearbeiten]

ProcessWire - Navigation Snippets

Seiten[Bearbeiten]

http://processwire.com/api/variables/pages/

Inhalt einer Seite mit $page->get()[Bearbeiten]

Beispiel

<p>Visit us at: 
<?php$contact = $pages->get("/about/contact/");
echo $contact->address;
?>
</p>

Kürzer:

<p>Visit us at: <?=$pages->get("/about/contact/")->address?></p>

Seiten Filtern find()[Bearbeiten]

"Featured" Checkbox im Backend angehakt[Bearbeiten]

<ul>
<?php$features = $pages->find("featured=1");
foreach($features as $feature)
   echo "<li><a href='{$feature->url}'>{$feature->title}</a></li>";
?>
</ul>

Beispiel: Limitierte Ausgabe, Sortieren und in Unterverzeichnis[Bearbeiten]

$pages->find("parent=/about/press/, featured=1, limit=3, sort=-date");

oder

$pages->get("/about/press/")->children("featured=1, limit=3, sort=-date");

Finde in Kategorie

$pages->get("/about/press/")->find("featured=1, limit=3, sort=-date");

Seiten mit Template xyz[Bearbeiten]

$pages->find("template=press_release, featured=1, limit=3, sort=-date");

Ausführliches Beispiel mit Ausgabe

<ul>
<?php$features = $pages->find("template=press_release, featured=1, limit=3, sort=-date");
foreach($features as $feature) {
  echo "<li>" .
    "<h3><a href='{$feature->url}'>{$feature->title}</a></h3>" .
    "<span class='date'>{$feature->date}</span>" .
    "<p>{$feature->summary}</p>" .
    "</li>";}
?></ul>

Felder[Bearbeiten]

Label zu Feldes und Subfeldern ausgeben[Bearbeiten]

Beispiel mit dem Combo Profield. Das Prinzip funktioniert bei allen Feldern.

https://processwire.com/talk/topic/26183-getting-label-of-combo-subfield/#comment-217749
$field = $fields->get('your_combo_field_name');
$settings = $field->getComboSettings();
$subfield = $settings->getSubfield('your_subfield_name');

// get label in current user language
$label = $subfield->getLabel();

// to get in a language other than current user language
$label = $subfield->getLabel('de');

// to get localized description
$description = $subfield->getDescription();

// to get localized notes
$notes = $subfield->getNotes();

// also note you can use getLanguageValue
$label = $subfield->getLanguageValue('de', 'label');

Label eines Felds ausgeben[Bearbeiten]

Geht über das Feld-Objekt.

echo $fields->get("body")->label;

Ist ein Feld leer ?[Bearbeiten]

if($page->summary_de) { ... }
// check whitespace
if(trim($page->summary_de)) { ... }

Datumsfelder formatieren[Bearbeiten]

 
// aus Datumsfeldern
echo date("Y-m-d", $page->getUnformatted("closing")); 

// aus Unix Timestamp mit Vanilla PHP
echo date("Y-m-d", $news->created);

setlocale(LC_TIME, "de_DE.utf8");
$weekday = strftime ( '%A' , $page->getUnformatted("date") );
$date = date("d.m.Y", $page->getUnformatted("date"));

Beispiel aus ProcessWire Blog

setlocale(LC_ALL, array('de_DE','de_DE.iso88591','de_DE.iso885915@euro','de_DE.utf8','de_DE@euro'));
$date = $page->date ? date('d.m.Y',$page->getUnformatted("date")) : date('d.m.Y',$page->getUnformatted("createdStr"));

Optionen eines Select Felds ausgeben (fieldtypeOptions)[Bearbeiten]

$options = $fieldtypes->get('FieldtypeOptions')->getOptions('YOURFIELDNAME'); 
foreach($options as $option) {
  echo $option->id;
  echo $option->value;
  echo $option->title; 
}

Parent von Repeater oder RepeaterMatrix finden[Bearbeiten]

Nutzt man ein Repeater Feld muss man manchmal die Seite herausfinden in der das Feld enthalten ist. Hierfür gibt es die Funktion getForPage().

if( get_class($myPage) == 'ProcessWire\RepeaterMatrixPage' || get_class($myPage) == 'ProcessWire\RepeaterPage') {
            $forPage = $myPage->getForPage();
            //var_dump( $myPage->getForPage());
            return $myPage = getImagePage($forPage);
}

Falls man Repeater in Repeatern hat kann man auch eine rekursive Funktion nutzen:

if (!function_exists('getForPage')) {
    
  function getForPage($myPage, $level=1){
      // if in repeater or repeaterMatrix find the first parent ProcessWire\Page Page
      $maxLevel=5;
      if( get_class($myPage) == 'ProcessWire\RepeaterMatrixPage' || get_class($myPage) == 'ProcessWire\RepeaterPage') {
          $level +=1;
          if($level > $maxLevel) return false;
          $forPage = $myPage->getForPage();
          //var_dump( $myPage->getForPage());
          //echo("<p>next Level: ".get_class($forPage)."</p>");
          return $myPage = getForPage($forPage);
         
      }else if( get_class($myPage) == 'ProcessWire\Page'){
          
          return $myPage;
      }
  }
}

Searchbar[Bearbeiten]

	<!-- search form-->
	<form class='search' action='<?php echo $pages->get('template=search')->url; ?>' method='get'>
		<input type='text' name='q' placeholder='Search' value='<?php echo $sanitizer->entities($input->whitelist('q')); ?>' />
		<button type='submit' name='submit'>Search</button>
	</form>

Create Admin User[Bearbeiten]

Reset Password - Passwort zurücksetzen[Bearbeiten]

<?php
require "index.php";
$admin = $users->get('admin1'); // or whatever your username is
$admin->setAndSave('pass', 'geheim');
?>
https://processwire-recipes.com/recipes/resetting-admin-password-via-api/

Problem For some reason, you have managed to lock yourself out of a site you are currently developing.

Solution Paste the following into a file (e.g. "reset.php") in the root folder of the site, then run it.

ProcessWire version >= 2.6.9

require "index.php";
$admin = $users->get('admin'); // or whatever your username is
$admin->setAndSave('pass', 'yo123456');

ProcessWire version < 2.6.9

require "index.php";
$admin = wire('users')->get('admin');
$admin->setOutputFormatting(false);
$admin->set('pass', 'yo12345');
$admin->save('pass');

Editor Link / Login Logout Link[Bearbeiten]

_main.php

<div class="editor">
<?php 
  if($user->isLoggedin()) {
    // if user is logged in, show a logout link
    echo "<a href='{$config->urls->admin}login/logout/'>Logout ($user->name)</a>";
  } else {
    // if user not logged in, show a login link
    echo "<a href='{$config->urls->admin}'>∆</a>";
  }
  // output an "Edit" link if this page happens to be editable by the current user
  if($page->editable()) echo "<li class='edit nav'><a href='$page->editUrl'>Edit</a></li>";
?>
</div>

Siehe auch ProcessWire - User Login / Logout

Conditions[Bearbeiten]

Bedingungen durch Oder Operator in Feldern[Bearbeiten]

Wenn das eine Feld leer ist nimm das nächste...

$t = $page->get("long_title|title");

Conditions für Navigation[Bearbeiten]

// If the page has children, then render navigation to them under the body.
// See the _func.php for the renderNav example function.
if($page->hasChildren) {
	$content .= renderNav($page->children);
}


// if the rootParent (section) page has more than 1 child, then render 
// section navigation in the sidebar
if($page->rootParent->hasChildren > 1) {
	$sidebar = renderNavTree($page->rootParent, 3) . $page->sidebar; 
}

Rendering[Bearbeiten]

Es gibt viele Möglichkeiten deshalb ein eigenes Kapitel:

ProcessWire - Render Funktionen

$page Object[Bearbeiten]

Siehe auch ProcessWire - Page Object

Zugriff auf $page in Funktionen[Bearbeiten]

Beispiel:

function getTitle() {
    $page = wire("page");
    $t = $page->title;

oder einfach übergeben.

Variablen zu Render-Datei senden[Bearbeiten]

// you can send vars (here item count)
// use this inside the field rendering script
// ie. $page->_n
$page->set('_n',$n);
$page->set('_type',$item->type); 
$out .= $item->render();

Copy and Paste[Bearbeiten]

Einfache Datumssteuerung[Bearbeiten]

Setzt Felder date_begin und date_end voraus.

// Date check
$dateValid = true;
$now = time();
$start = 0;
$end = 0;
if($page->date_begin){
  $start = strtotime( $page->date_begin('Y-m-d') . " 00:00:00");
  if( !($now >= $start) ) $dateValid = false;
}
if($page->date_end){
  $end = strtotime( $page->date_end('Y-m-d') . " 23:59:59");
  if( ($now > $end) ) $dateValid = false;
}

if(!$dateValid) return '';

Copyright / Published Date[Bearbeiten]

$published_date = date('d.m.Y',$page->created);
<?= '©'.$published_date ?>

Stylesheets und Skripte mit AIOM[Bearbeiten]

<script src="<?php echo AIOM::JS(array('scripts/jquery-2.2.4.min.js','scripts/jquery.chocolat.js','scripts/jquery.fitvids.js','scripts/jquery.flexslider-min.js', 'scripts/main.js')); ?>"></script>
<link rel="stylesheet" type="text/css" href="<?php echo AIOM::CSS('styles/main.less'); ?>">
<?php echo $homepage->site_title .' '. Date('Y'); ?>

Link zur Seite anhand URL-Pfad[Bearbeiten]

<?php echo $pages->get('/ueber-uns/')->url; ?>

Feld einer Seite[Bearbeiten]

$content_left = $page->get('content_left');

Bild[Bearbeiten]

if($page->main_image){$image = $page->main_image->size(640,960);}
else $image = NULL;

Bild von der Homepage[Bearbeiten]

in _init.php

$homepage = $pages->get('/');

im Template

if($homepage->config_logo){$logo = $homepage->config_logo->width(280);}
else $logo = NULL;

Simple Navigation[Bearbeiten]

<ul class='nav-list'>
  <?php
  // Childrens
  foreach(($page->children) as $item) {
    if($item->id == $page->rootParent->id) {
      echo '<li class="current">';
    } else {
      echo '<li>';
    }
    echo '<a class="ajax-link" href="'.$item->url.'">'.$item->title.'</a></li>';
  }
  ?>
</ul>

Auflistung mit div. Feldern aus Kindseiten[Bearbeiten]

/**
 * Publications list template
 *
 */
$publications = '';
$children = $page->children;
foreach($children as $child){
  $publications .= '<div class="box">';
  $publications .= '<h2><a class="ajax-link" parent="'. $child->parent->title .'" name="'.$child->title.'" href="'. $child->url. '">'. $child->title . '</a></h2>';
  $publications .= $child->body;
  $publications .= '</div>';
}

$content = $page->body . $publications;

Minigalerie[Bearbeiten]

Thumbnails / Child Pages

$works = '<div class="gallery">';
$children = $page->children;
foreach($children as $child){
  $works .= '<a class="ajax-link" parent="'. $child->parent->title .'" name="'.$child->title.'" href="'. $child->url. '">';
  $works .= '<h2>'. $child->title . '</h2>';

  if(count($child->thumbnail)){
      $works .= '<div class="thumbnail"><img src="'. $child->thumbnail->getThumb('thumbnail') .'" title="'. $image->description .'"></div>';
  }

  $works .= '</a>';
}
$works .= '</div>';


$content = $page->body . $works . '<br/>' . $page->video;

Page Navigation[Bearbeiten]

// 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>';
}

$content = '<h2>'. $page->title . '</h2>' . $page->body . '<br/><div class="video">' . $page->video .'</div>' . $next;

Simple Gallery with next Page[Bearbeiten]

$gallery = '<div class="gallery">';
$images = $page->gallery;

// add thumbnail to gallery
if(count($page->thumbnail)){
    $gallery .= '<a class="chocolat-image gallery-item" href="'.$page->thumbnail->url.'"><img src="'. $page->thumbnail->getThumb('thumbnail') .'" title="'. $image->description .'"></a>';
}

foreach($images as $image){
    $gallery .= '<a class="chocolat-image gallery-item" href="'.$image->url.'"><img src="'. $image->getThumb('thumbnail') .'" title="'. $image->description .'"></a> ';
}
$gallery .= '</div>';

// 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>';
}


$content = '<h2>'. $page->title . '</h2>' . $page->body . '<br/>' . $gallery . '<br/><div class="video">' . $page->video .'</div>'. $next;

Platzhalter-Seite mit Redirect auf erste Unterseite[Bearbeiten]

Möchte man eine Eltern-Seite für das Menü generieren die keinen eigenen Inhalt besitzt sondern nur Unterseiten enthält, kann man ein Template für diese Seite erstellen, dass den User auf die erste Unterseite weiterleitet.

Diese Lösung ist ein regulärer Redirect. Ob das die Beste Lösung ist müßte man mal überlegen. Auf alle Fälle besser als Duplicate Content.

<?php namespace ProcessWire;
$root = $pages->get("/");
if($page->numChildren){
	$session->redirect($page->child()->url);
} else $session->redirect($root->url);
?>

In Funktionen auf Page Objekt zugreifen[Bearbeiten]

Entweder als Parameter übergeben (evtl. by reference) oder wenn ein ProcessWire Objekt in der Funktion vorliegt das vom Wire Objekt abgeleitet ist (fast alle) kannst du dieses nutzen:

$page = $items->wire('page'); // $items is a PageArray - use its wire to get current page

ProcessWire - Debugging Snippets[Bearbeiten]

ProcessWire - Debugging Snippets