Forum

Thread tagged as: Question

[Portfolio] fetching latest item + related items from same category

Hi there,

First of all, thanks for the great tutorial for creating a portfolio. Very useful ressource.

I have two questions:

1. Fetching latest item from portfolio

I'd like to display the latest added item from the portfolio on my homepage. I know it's possible to do so using the blog app through sort-order; is there any way to achieve the same thing through categories? For now, I have to re-order the items in the back-office in order to get it to work.

Here is how the index.php looks like:

<?php
     perch_content_custom('Portfolio', array(
          'count' => 1,
          'template' => 'portfolio_listing.html',
          'page'=>'/portfolio.php',
          'category' => perch_get('cat'),
     ));
?>

2. Related items

On the detail page of a particular portfolio item, I'd like to display the related items belonging to the same category. Is this achievable? Do you have any leads?

Thanks, Robin

Robin Pick

Robin Pick 5 points

  • 7 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

  1. You should be able to add:
'sort' => '_order',
'sort-order' => 'DESC',
  1. Yes, you need to get the categories for the current item, and then feed those back into another perch_content_custom() call to get items in those categories. Filter it so that the item's _id isn't in the set.

Thanks for the prompt reply.

I am now able to get the latest portfolio item; noted for the related items, I'll try to implement this.

Hi,

I'm able to get related items by hard-coding a value for the category:

perch_content_custom('Episodes', array(
       'page'=>'/episodes.php',
       'category' => array('themes/friendship')
));

One thing I'm not able to do is to get the categories of the current item. Any hints?

Cheers, Robin

Drew McLellan

Drew McLellan 2638 points
Perch Support

Why are you not able to do that?

I'm actually not too sure how to do it, I believe I am missing something somewhere :) I don't know how to access the path of the categories of a given item.

The following code fetches all the entries regardless of categories rather than filtering the ones from the category of current item.

$categories = perch_content_custom('Episodes', array(
     'filter' => 'slug',
     'match' => 'eq',
     'category' => perch_get('cat'),
     'template' => 'episodes/related.html',
));
Drew McLellan

Drew McLellan 2638 points
Perch Support

Use skip-template to get the current categories

$result = perch_content_custom('Portfolio', array(
          'count' => 1,
          'template' => 'portfolio_listing.html',
          'page'=>'/portfolio.php',
          'category' => perch_get('cat'),
          'skip-template' => true,
     ));

Then $result[0] will be your item, and presumably something like $result[0]['category'] will be your categories.

Thanks for the reply, this is helpful. I get the array with all the information from the region using $result[0]. Nevertheless, when I specify the following:

$result[0]['category']

It seems that I'm only getting the id of the category, there's no mention of the name of the categories.

Array ( [0] => 4 [1] => 6 )

Am I missing something?

Drew McLellan

Drew McLellan 2638 points
Perch Support

No, that's something we'll need to look at.

Drew McLellan said:

No, that's something we'll need to look at.

For the time being, is there a way to map the id of the categories to the category name?

Drew McLellan

Drew McLellan 2638 points
Perch Support

I don't believe so.

Ok, is this something you might fix eventually? I find that it would be really useful to be able to access the categories assigned to a particular item.

Cheers,

Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, absolutely.

Hi Drew,

Just wondering if you made any progress on this? The use case is to append the categories assigned to an item to both description and title meta tags.

Cheers,

Drew McLellan

Drew McLellan 2638 points
Perch Support

No, it's not something I've had time to look at.

Thanks for the fix!

Hi,

I have an problem getting the translated values in case of multi-lingual categories.

Here is how I set it up: I have a set of category (themes) which contains categories (perseverance, parenthood, etc.). Each of these categories has two fields for each translations.

I do get the category name but the array doesn't give me the translated text of a given category.

$results = perch_content_custom('Episodes', array(
      'skip-template' => true,
      'filter' => 'slug',
      'match' => 'eq',
      'value' => perch_get('s'),
      'count' => 1
));

$themes = $episodesResults[0]['themes'];

When I print the array, I get the name of the categories but not the translated texts.

Array ( [0] => themes/parenthood/ [1] => themes/marriage/ )

How could I go one level deeper and get the translated texts?

Cheers,

Drew McLellan

Drew McLellan 2638 points
Perch Support

Those are the category paths - you should be able to use them to look up any information about the category.

Could you give me a hint on how to do that?

I know how to filter content based on a given categories but somehow I don't understand how to access the information (in my case the two translations) from a specific category.

Thanks,

Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, you can use the perch_category() function - pass it the category path:

perch_category('colours/blue')

Ok thanks. I do get the two translated value by doing this:

perch_category('themes/perseverance/');

I would like now access either one of the two translations depending on the language set by the user. Usually, I can skip-template and get the array and play around with what I need. Here, it does not seem to do the trick. I believe I'm doing something which makes no sense here, as the page becomes white.

$themes = perch_category('themes/perseverance', array(
      'skip-template' => true
));

The doc states that perch_category accept an array of options. Is there an equivalent of skip-template for the perch_category? What to do to be able to print one of the two translations?

Thanks for your help!