Forum

Thread tagged as: Question

How to display content from a category template?

Hi

I'm using this in a template, so the php below can filter the content based on a slug:

 <a href="/project/<perch:content id="slug" type="slug" />">link</a>

This displays a single item filtered on the slug above:

 perch_content_custom('Projects', array(
 'page' => '/projects.php',
 'template' => 'rc_project_detail.html',
 'filter' => 'slug',
 'match' => 'eq',
 'value' => perch_get('s'),
 'count' => 1,

This works, but I'd like to display some additional content.

The single item is linked to a category, each category has a brief description. I'd like to pull in the category description when viewing the single item.

I've added perch_categories to the php

 perch_categories(array( 
 'set'=>'projects',
 'template'=>'category_project_single.html',
 'count' => 1,
  ));

 perch_content_custom('Projects', array(
 'page' => '/projects.php',
 'template' => 'rc_project_detail.html',
 'filter' => 'slug',
 'match' => 'eq',
 'value' => perch_get('s'),
 'count' => 1,
 ));

This displays only the first category description, not the one related to the item.

How do I display the related category description?

Stephen Meehan

Stephen Meehan 4 points

  • 7 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Put the category tags in your template. You don't need to do it with PHP - the template will do it.

Ah, clever.

I forgot I had to wrap the perch:category with perch:categories

  <perch:categories id="our-projects" label="Project categories" set="projects" required="true" display-as="checkboxes">  
  <header class="page_intro">
  <h1><perch:category id="desc"/></h1>
 </header>
  </perch:categories>

I've ran into a small issue. Some of the items are in more than one category, this displays multiple category descriptions. Which makes sense, but isn't the desired outcome.

Is there a way to output a single category description based on the current category?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Use <perch:if exists="perch_item_first">...</perch:if> in your template.

Great stuff, thanks for the swift response.

<perch:if exists="perch_item_first">...</perch:if> helps with the original problem of all the category descriptions being displayed, but unfortunately it creates another.

This outputs the perch:category id="desc" from my category template

    <perch:if exists="perch_item_first"><h1><perch:category id="desc"/></h1></perch:if>

The problem is when an item has been assigned more than one category, only the first category description is displayed, regardless of how many categories the item is linked to. This means sometimes the wrong category description is shown.

Is there a way to tell Perch to only display the category description based on the current category?

I feel like maybe I need to use something like this (this outputs all the category descriptions) but I don't know how to pass the current category in to the array. Hope you can help.

     perch_categories(array( 
      'set'=>'projects',
      'template'=>'category_project_single.html',
      'count' => 1,
       ));
Drew McLellan

Drew McLellan 2638 points
Perch Support

I don't really understand what you're trying to do.

I have setup a multi-item region with categories.

I'm using a category template that contains a field called category description.

I'd like to be able to display the category description based on the current category.

I was trying (unsuccessfully) to do this with PHP, but you kindly pointed out it can be done by using perch:category within the HTML template.

      <h1><perch:category id="desc"/></h1>

This works, but if an item has more than one category associated with it, say, category-1 and category-2 both category descriptions are displayed.

You then suggested (great support by the way) I use perch_item_first

     <perch:if exists="perch_item_first"><h1><perch:category id="desc"/></h1></perch:if>

When I view an item that's categorised as category-1 and category-2 I'll always see the category description for category-1 because I'm using perch_item_first

perch_item_first only displays the first category description, it doesn't display the related category description.

The question is:

How do I output the related category description not just the first?

Hi

Did my previous post explain the problem any clearer?

Bump...I don't know why this was marked as solved....regardless, I'm having the same problem. What Stephen was basically asking is:

Say you have a photo gallery with category FLOWERS and category PLANTS. You click on the "PLANTS" so it sorts it by PLANTS. How do you display "PLANTS" on that page in the breadcrumbs (Homepage > Photos > "plants")? You try to use perch:categories but it just displays all of your categories available to you. If you use perch_item_first it will only display the first category in the set, regardless of whether or not you selected PLANTS.

How do I get it to simply display "plants" in the template?

Thanks!