Forum

Thread tagged as: Question, Problem

Similar items category filter

Hi there,

I am building a site for a hotel and am using list and detail mode to display the hotel rooms. Each room is assigned to one of two categories: Sea View or Garden View. There are 4 rooms for each category. Within the detail page I would like to display the other three remaining rooms for that category as alternatives for the user to select. I currently can't get this to output on the detail page using perch_get('cat'). However it does output if I specify the category path eg: rooms/sea-view-rooms.

Any help with how to output only the remaining 3 rooms within that category, so ignoring the one that is currently displayed would be greatly appreciated!

Here are my templates:

room.php:

<?php
  perch_content_custom('Rooms', array(
       'page' => '/guestrooms.php',
       'template' => 'room_detail.html',
       'filter' => 'slug',
       'match' => 'eq',
       'value' => perch_get('s'),
       'count' => 1,
  ));

  perch_content_custom('Rooms', array(
       'page' => '/guestrooms.php',
       'template' => 'room_list.html',
       'category' => perch_get('cat'),
  ));
?>

room_detail.html:

<perch:content id="slug" for="roomTitle" type="slug" suppress="true" />
<perch:categories id="roomCat" label="Category" set="rooms" />

<perch:template path="content/hero.html" />

<div class="room">
    <div class="page-width">

      <div class="row">
        <h2 class="room__title title-strike"><span><perch:content id="roomTitle" type="text" label="Room Title" title="true" /></span></h2>

      <perch:template path="content/quote.html" />

      <div class="page-width page-width--content">
        <div class="row--pad-medium-bottom">
          <perch:template path="content/carousel.html" />
        </div><!--/row-->
      </div><!--/page-width-->


      <div class="page-width page-width--content">
        <div class="row--pad-medium-bottom">
          <div class="room__details">

            <div class="room__details__copy">
              <div class="rte">
                <perch:content id="roomCopy" type="textarea" html="true" editor="redactor" label="Room Description" />
              </div><!--/rte-->
            </div><!--/room__details__copy-->

            <div class="room__details__table">
              <perch:template path="content/prices.html" />
            </div><!--/room__details__table-->

          </div><!--/room__details-->
        </div><!--/row-->
      </div><!--/page-width-->

      <div class="row--pad-medium-bottom">
        <perch:categories id="roomCat" set="rooms" />
          <h2 class="rooms__title title-strike"><span>Other <perch:category id="catTitle"/></span></h2>
        </perch:categories>
      </div><!--/row-->

    </div><!--/page-width-->
  </div><!--/row-->
</div><!--/room-->

room_list.html:

<perch:before>
  <div class="page-width page-width--content">
    <ul class="rooms__list">
</perch:before>

      <li class="rooms__list__item">
        <a href="room/<perch:content id="slug" type="slug" />">
          <img class="rooms__list__item__image" src="https://placehold.it/600x600" alt="room" />
          <div class="rooms__list__item__overlay">
            <img class="rooms__list__item__overlay__image" src="https://placehold.it/300x300" alt="country" />
            <h3 class="rooms__list__item__overlay__title"><perch:content id="roomTitle"/></h3>
          </div><!--/rooms__list__item__image-->
        </a>
      </li>

<perch:after>
    </ul><!--/rooms__list-->
  </div><!--/page-width-->
</perch:after>

Many thanks!

Rob Saunders

Rob Saunders 0 points

  • 3 years ago

I've managed to get the related categories to display but I can't work out how to ignore the current item that is being viewed and just output the other two. This is how I got it working so far:

  $category =  perch_content_custom('Rooms', array(
       'page' => '/guestrooms.php',
       'template' => 'room_detail.html',
       'skip-template' => 'true',
       'return-html'   => 'false',
  ));

  $categoryPath = $category[0]['roomCat'];

  perch_content_custom('Rooms', array(
      'page' => '/guestrooms.php',
      'template' => 'room_list.html',
      'category' => $categoryPath
  ));

Think I fixed it, for anyone else this worked for me on the detail.php page:

 <?php
    $category =  perch_content_custom('Rooms', array(
         'page' => '/guestrooms.php',
         'template' => 'room_detail.html',
         'filter' => 'slug',
         'match' => 'eq',
         'value' => perch_get('s'),
         'count'    => '1',
         'skip-template' => 'true',
         'return-html'   => 'false',
    ));
    $categoryPath = $category[0]['roomCat'];
    perch_content_custom('Rooms', array(
        'page' => '/guestrooms.php',
        'template' => 'room_list.html',
        'category' => $categoryPath,
        'filter' => 'slug',
        'match' => 'neq',
        'value' => perch_get('s'),
        'count'    => '3',
        'sort'     => 'roomTitle',
        'sort-order' => 'RAND'
    ));
  ?>
Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok, great.