Forum

Thread tagged as: Question

Different content on detail page based on category

Hi,

I have the following scenario on a Perch site.

library.php This is a listing and detail setup with categories which is based on the tutorial video Portfolio with Categories. This page displays ALL items regardless of category and works as needed.

young-investor.php This is a page that ONLY lists items with the category 'young-investor' using the following code:

<?php
    perch_content_custom('Library', array(
      'page' => '/library.php',
      'template' => 'library_listing.html',
      'category' => 'tags/young-investor',
      'paginate' => true,
      'count' => 4,
    ));
?>

Both of the listing pages link to the same detail page detail.php with the following code:

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

Is there a way of displaying (either via the page or template) slightly different HTML on the detail.php page if the item is tagged 'young-investor'?

Thanks, Andy

Andy Knight

Andy Knight 1 points

  • 6 years ago
Rachel Andrew

Rachel Andrew 394 points
Perch Support

You could use perch:if and then display different HTML etc.

https://docs.grabaperch.com/docs/templates/conditionals/if/

Hi Rachel,

Thanks for the response.

I had tried perch:if but was struggling to get it to work. Here is the code I was trying:

<perch:if id="tag" match="contains" value="young-investor">
  ... Do stuff
</perch:if>

Any ideas?

Hi Rachel,

Sorted - the if statement needed to be in the perch:categories tags and I change the matching a bit as well.

<perch:categories id="tags" label="Tags" set="tags">
    <perch:if id="catTitle" match="in" value="Young Investor">
        ... do stuff
        <perch:else />
        ... do other stuff
    </perch:if>
</perch:categories>

Hi,

I have another issue now using the code above.

If something has a category 'Young Investor' and 'Apple' etc both 'do stuff' and 'do other stuff' are displayed. Additionally, if an items has the categories 'Apple' and 'Banana' 'do other stuff' is displayed twice.

How can modify this to 'do stuff' OR 'do other stuff' once?

Andy

Drew McLellan

Drew McLellan 2638 points
Perch Support

The code inside the <perch:categories> tags effectively loops through for each assigned category, so I'm not sure that's going to work.

Hi Drew,

Thanks for the info - Can you suggest an alternative way of doing this?

Drew McLellan

Drew McLellan 2638 points
Perch Support

I can't off the top of my head, but I'll have a think about it.

OK great, thanks Drew.