Reorder fetched content


#1

Hi, I am showing selected projects on a homepage with the checkboxes, which is working pretty well. I was curious if there is an easy way to reorder projects, instead of having the selected, one after another. I just tried some sorting options but I want a bit more control of the order.

Its currently based on this

<?php foreach($projects as $project) : ?>
		    <?php if($project->selectedpage()->bool()): ?>	
						<!-- Project Thumb -->
			<?php endif ?>
<?php endforeach?>

selectedpage:
    label: Selected Page
    type: checkbox
    text: Show project on the selected page

#2

Hm, what would be your criteria for sorting?

You could use a completely different setup with a. structure field in the homepage where you could then select the projects to. show on the page. The structure entries could then be sorted manually in the Panel.

On a side note: I would filter the projects instead of looping through all and then checking if the condition is true.

So. instead of this piece of code

<?php foreach($projects as $project) : ?>
		    <?php if($project->selectedpage()->bool()): ?>	
						<!-- Project Thumb -->
			<?php endif ?>
<?php endforeach?>

use this:

<?php
$projects = page('projects')->children()->filterBy('selectedpage', true);
 foreach($projects as $project) : ?>
  <!-- Project Thumb -->
<?php endforeach?>

#3

Thanks for the filter tip.

Your recommendation sounds good. What would be the best way to set this up? Does the pages + files has to duplicated for this to see those on my homepage in the panel? Because on my other subpages there are already the order I need for the other page overviews.


#4

I’d set up a structure field like this:

selectedpages:
  label: Selected Pages
  type: structure
  fields:
    selectedpage:
      label: Select a page...
      type: select
      options: query
      query:
        page: projects
       fetch: children
       text: '{{title}}'
       value: '{{uri}}'

So you have a single select field in your structure. These entries can then be sorted by dragondropping them around.

In your template, you. can fetch them like this:

$selectedPages = $page->selectedpages()->toStructure();
foreach($selectedPages as $item) {
   // lets make. sure we have a page object
  if( $project = $item->selectedpage()->toPage()) {
    echo $project->title();  // or whatever you want to fetch. here
   // from here on, you can deal with the page in the same way you would anywhere else
  }
}

No, you. just store a reference to the pages.


#5

Thanks! So the structure field has to placed into my homepages blueprint?
Its showing me suapages but nothing is happening when I click them


#6

okay, something was wrong. The blueprint is working now.


#7

Thats exactly I was looking for. Thank you so much.
Is it also possible to get subpages listed of two different pages or just for one or all pages?


#8

If you want to query multiple pages, you would have to use the API approach (section Dynamic options via API) or theControlled List plugin


#9

Great! I will give it a try.