Search - Show Results from a Filter on a Page Only

Like

<?php

use Kirby\Cms\Page;

class ItemsPage extends Page
{

    public function children()
    {
        $itemdetail = [];

        foreach ($page->items()->toStructure() as $item) {
            $itemdetail[] = [
                'slug'     => $this->slug() . $item->itemnumber(),
                'template' => 'sidemenuproducts',
                'model'    => 'sidemenuproducts',
                'content'  => [
                    'title'  => $this->title() . '(' . $item->itemnumber() . ')',
                ]
            ];
        }

        return Pages::factory($itemdetail, $this);
    }

}

Like this?

<?php

use Kirby\Cms\Page;

class SideMenuProductsPage extends Page
{

    public function children()
    {
        if ($this->children instanceof Pages) {
            return $this->children;
        }

        $itemdetail = [];

        foreach ($this->items()->toStructure() as $item) {
            $itemdetail[] = [
                'slug'     => $item->itemnumber(),
                'template' => 'sidemenuproducts',
                'model'    => 'sidemenuproducts',
                'content'  => [
                    'title'  => $this->title() . ' (' . $item->itemnumber() . ')',
                ]
            ];
        }

        return $this->children = Pages::factory($itemdetail, $this);
    }

}

The .txt file is also named correctly, I’m still getting a 404.

Screenshot 2023-03-17 at 4.00.04 PM

Screenshot 2023-03-17 at 4.03.56 PM

404 for what?

If it type in for example the url followed by an existing item number.

trishkaufmann.test/4437

But if I look at your folder structure, the url for that new virtual page should be

items-for-sale/confederate-postal-history/csa-1-on-cover-section-1/4437

Please have a look at how Kirby treats nested content folders url wise.

1 Like

Thank you. That is working and I updated the template within the model so that I can control what is shown.

All that is need on the template is to use the structured fields with the $page variable to pull in the data wanted.

Example

<p><?= $page->itemnumber() ?></p>

Updated Model

<?php

use Kirby\Cms\Page;

class SideMenuProductsPage extends Page
{

    public function children()
    {
        if ($this->children instanceof Pages) {
            return $this->children;
        }

        $itemdetail = [];

        foreach ($this->items()->toStructure() as $item) {
            $itemdetail[] = [
                'slug'     => $item->itemnumber(),
                'template' => 'itemdetail',
                'model'    => 'sidemenuproducts',
                'content'  => [
                    'title'  => $this->title() . ' ' . 'Item#' . ' ' . $item->itemnumber(),
                    'image' => $item->image(),
                    'itemnumber' => $item->itemnumber(),
                    'fulldescription' => $item->fulldescription(),
                    'price' => $item->price(),
                ]
            ];
        }

        return $this->children = Pages::factory($itemdetail, $this);
    }

}

New “Item Detail” template so far

<?php snippet('head') ?>

<?php snippet('header') ?>

<div id="content_container">
	<div class="grid-x grid-margin-x">
		<div id="page_title" class="cell">
			<?= $page->title() ?>
        </div>
		<div id="page_content" class="cell">
            <?= $page->image()?>
            <p><?= $page->itemnumber() ?></p>
            <p><?= $page->fulldescription() ?></p>
            <p><?= $page->price() ?></p>
        </div>
	</div>
</div>

<?php snippet('footer') ?>

Output

The search now works to find the item detail pages now as well!

Search Template

<?php if ($results->isNotEmpty()): ?>
                <strong>Search Results:</strong>

                <ul>
                    <?php foreach ($results as $result): ?>
                    <li>
                        <a href="<?= $result->url() ?>">
                            <?= $result->title()->itemnumber() ?>
                        </a>
                        <?= $result->shortdescription() ?>
                    </li>
                    <?php endforeach ?>
                </ul>
            <?php else: ?>
                <strong>Results:</strong> <br>
                No results found.
                
            <?php endif ?>
            
            <div class="button-group align-center">
                <?php if ($results->pagination()->hasPages()): ?>
                    <?php if ($results->pagination()->hasPrevPage()): ?>
                        <a class="brand-button" href="<?= $results->pagination()->prevPageURL() ?>">
                            ‹ previous items
                        </a>
                    <?php endif ?>

                    <?php if ($results->pagination()->hasNextPage()): ?>
                        <a class="brand-button" href="<?= $results->pagination()->nextPageURL() ?>">
                            more items ›
                        </a>
                    <?php endif ?>
                <?php endif ?>
            </div>

Just styling left to do!