Jump to content

Page Reference Field Item Number?

Recommended Posts

Hello everyone,

Is there a way to access the key number of an item in a page reference field and then output it as a value?
So for example I want to automatically output the number 1 if the referenced page is the first in the list and have it be updated when the list is reordered via drag and drop.

Thanks in advance!

Share this post

Link to post
Share on other sites
6 hours ago, wesp said:

and then output it as a value

In the frontend? Or right in the backend GUI, whenever you re-order page references?

Share this post

Link to post
Share on other sites

Mainly in the frontend, but both would actually be nice, if that's possible

Share this post

Link to post
Share on other sites
22 hours ago, wesp said:

Is there a way to access the key number of an item in a page reference field and then output it as a value?

You're nearly answering your own question there. The key is the position number in the field (zero-indexed like all arrays).

Using this field as an example...


The key is the position:



  • Thanks 1

Share this post

Link to post
Share on other sites

yes that works!! thanks

the => command was what I was missing, I had actually tried to get the $key with a bunch of other commands and didn't know about this one

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Manuel
      Hi everybody!
      I'm new to PW and just did the beginner-tutorial “Hello Worlds”, a beginning ProcessWire tutorial --> Link
      After extending the template from the original tutorial with a page_reference-Field (Page-Field value type is configured as single page),
      PW throws a fatal error when i try to output multiple pages:
      $planets = $pages->find("template=planet, sort=-title"); foreach($planets as $planet) { echo $planet->title; echo $planet->planet_reference->title; } Should return three Pages, but throws the following error:
      When i try to output the field with a single page, everything just works fine:
      $planet = $pages->get(1018); echo $planet->title; echo $planet->planet_reference->title; I've tried to solve the problem with myself (and google), but i can't fix the error.
      Thanks a lot!

      My enviroment:
      OSX Mojave / MAMP (PHP 7.2), PW v 3.0.123
    • By ttttim
      I'm working on a website where in the back-end in one template i have multiple page reference fields using the same conditions (parent and template). Is there a way if i selected a page in field A it won't show up anymore in the list of field B or field C etc?
      I was looking at creating a custom string but i couldn't wrap my head around it.
    • By dragan
      Is it somehow possible to limit the number of pages one can select with the page reference field?
      I have a case where I need to have exactly three, not more, not less. Is this doable with a hook? Or would you suggest another approach? (three single pageref fields and make each mandatory)
    • By timothy.m.wilson
      Good afternoon,
      I am attempting to loop through approximately 600 XML nodes. Each of those nodes has 2 child nodes (example below). One is an id for an existing page and the other contains values for a page reference field. I have put together a loop (below as well) to attempt to save each page with the label values. This all works....once. After getting the first XML node "label" saved to the correct page, nothing else is saved. I'm sure there is probably something that I have overlooked. But, I am also very new to this CMS. I'm hoping there is a veteran user who may be able to point me in the right direction. 
      foreach ($xml->row as $items) {
              //get page id to update and set output formatting to false
              $blog_page = $pages->get('id='.$items->id);
              //create array from pipe separated label values
              $labelsArray = explode('|', $items->labels);
              //loop through array to get page ids by page name
              $labelIDS = array();
              foreach ($labelsArray as $labelValue) {
                  $labelPage = $pages->get('name='.$labelValue);
                  array_push($labelIDS, $labelPage->id);
              //add labels to label field in post
              $blog_page->labels = $labelIDS;
              //save page
    • By cstevensjr
      Good Day.  I have created a repeater field on a website that I am building.  Three of the fields within the repeater uses page reference fields (I hope that's the correct terminology).   Everything works fine, except that I cannot get those three fields to display the Title and not the page IDs.  I've tried different syntax, however I cannot seem to get this to work.

      I used a repeater template (repeater_sd_note.php file), as Ryan had mentioned.  This works great.

      I have included these screenshots, that will hopefully help someone to guide me in the right direction.
      Screenshot 1 - A portion of what the web display looks like.
      Screenshot 2 - A portion of what the backend display looks like of the Screenshot 1 web page.
      Screenshot 3 - The sd_note repeater field contents.
      Screenshot 4 - The template file used on the web page.
      Screenshot 5 - A portion of the Zurb Foundation _main.php file that covers the rendering of the repeater.
      Screenshot 6 - The contents of the repeater_sd_note.php file.
      I have read quite a few forum articles and website documentation on repeaters, however I didn't find anything that I thought covered this subject. I apologize if a solution to this can be easily found.  I worked on this for two days already.  Thanks in advance.
      Using ProcessWire 2.4.1
  • Create New...