URL segments and the page reference field

Posted 16th Dec 2017

#api #bootstrap4 #completeguide #fields #selector #urlsegments

Admin > Setup > Templates

Create the 'cat-index', 'cat-entry' and 'cat-attribute' templates.

Creating the cat-index, cat-entry and cat-attribute templates
Creating the cat-index, cat-entry and cat-attribute templates Zoom

/site/templates/

Create the files 'cat-index.php' and 'cat-entry.php' files and create a new page called 'Cats' under 'Home' using the 'cat-index' template.

Creating the cat index page
Creating the cat index page Zoom

So, as things stand, you now have a path at '/cats/'. We can say this path resolves to a page on our site. That is, whatever code we put in 'cat-index.php' will now run at this URL. Now, this page has no child pages, therefore the path '/cats/persian/' does not resolve to a page on our site. Instead, this will generate a 404 error because the page does not exist.

We're going to make that URL (and others) work. That is, we're going to list our cats, within the same template 'cat-index.php', based on the end part of the URL which is this case was '/persian/'. If URL segments are enabled for the 'cat-index' template, and the path does not resolve to an actual page, then the '/persian/' part of the URL is known as URL segment number 1.

Now we can create a new page called 'Type' under 'Home' using the 'cat-attribute' template.

Creating the cat attribute page
Creating the cat attribute page Zoom

Now you could use two templates for this, one for the parent 'cat-attribute-index' and one for the children 'cat-attribute-entry'. This way you could easily get the types by:

<?php
 // get children (PageArray) of page created with cat-attribute index template
  $catTypes = $pages->find("template=cat-attribute-index")->children;
?>

However, you don't need to do this, you can just as easily get the children using the '/type/' path instead as this is a unique value (remember, paths and IDs are unique in processwire).

<?php
  // get children (PageArray) from path '/types/'
  $catTypes = $pages->get("/types/")->children;
?>

Next we need the page reference field which will reference the child pages under '/type/'. This will be used to select the type of cat when a page is created using the 'cat-entry' template.

Admin > Setup > Fields

Create a page reference field called 'cat-type'.

Create page reference field
Create page reference field Zoom

On the input tab, specify the input type as 'Radio buttons'.

Input tab choose input method
Input tab choose input method Zoom

Choose the parent of selectable pages as 'Type' and choose the template of selectable pages as 'cat-attribute'. You must select both of these in order to be able to create new pages from this field. Why you do this will become apparent later.

Input tab choose parent of selectable pages
Input tab choose parent of selectable pages Zoom

Allow new pages to be created from field? Yes, check this box.

Input tab allow new page creation from field
Input tab allow new page creation from field Zoom

Now you just need to set up the 'cat-entry' template. Add the new 'catType' field to this template, along with an image field which holds a single image.

Set up cat-entry template
Set up cat-entry template Zoom

Now you're all set and ready to create some content!

Feedback & support

I hope you enjoyed this tutorial. You can support pwtuts by following on @pwtuts. You can also donate at paypal.me/swcarrey to help support the site which would be awesome!

Related tutorials / See all

Suggest a tutorial

Please note: I do not store any of this information, it's simply used to send me an email. Your email address is required so I can get clarification on your request if needed.