Forum

Thread tagged as: Question

Output only one level of categories

Is it possible to output just one level of categories, excluding sub-categories?

Creative Monster

Creative Monster 0 points

  • 6 years ago

Category Filtering Once you have created categories and assigned them to Perch Content you can filter content by category or multiple categories.

Categories are filtered using their category path, which is made up using the set slug and the category slug. For example, if you wanted to filter by the category red in the set called colours, the path would be:

colours/red If there was a subcategory of red called scarlet, you would filter for that using:

colours/red/scarlet Example – filtering by a single category

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

Sorry, I don't think my original question was clear. I'm using perch_categories to output a list of categories, but at the moment I'm getting a list of all categories within a set plus any child categories.

I would like a list of just parent cats.

Ok, I'm pretty certain you would need to use the skip-template option then process the resulting array in php.

I would have to mock up a test to verify this though. Is this something you know how to do?

Yes perfectly fine doing that, I was just wondering if this could be done natively with perch.

An 'exclude-children' option to the perch_categories function would be useful here

Cheers Robert.

Yes, You can filter as you expected. You need to filter on "catDepth" would you like an example?

As in:

perch_categories(array(
  'filter'  => 'catDepth',
  'match'   => 'eq',
  'value'   => 1
));

Toby Martin said:

As in:

perch_categories(array(
 'filter'  => 'catDepth',
 'match'   => 'eq',
 'value'   => 1
));

Yes, this looks correct.

No, I tried this, I believe filter needs to be an ID of a field.

I'm now struggling to output a list of just sub-categories too.

I thought this would wok:

$subCats = perch_categories([
    'skip-template' => true,
    'set'     => 'products',
    'filter'  => 'catPath',
    'match'   => 'contains',
    'value'   => 'products/beds/'
]);

but it doesn't.

and if remove the trailing slash from 'value' I get the parent category as part of the list.

Drew McLellan

Drew McLellan 2638 points
Perch Support

What does debug output for that?

SELECT * FROM perch2_pages WHERE pagePath='/categories/index.php' LIMIT 1
SELECT setID FROM perch2_category_sets WHERE setSlug='products' LIMIT 1
SELECT main.* FROM perch2_categories main WHERE 1=1 AND (catPath REGEXP '[[:<:]]products\/beds\/[[:>:]]') AND setID='1' ORDER BY catTreePosition ASC
Using template: /templates/categories/category.html
Drew McLellan

Drew McLellan 2638 points
Perch Support

I would expect that to work. If not, I'll have to go back to the drawing board.

It just returns an empty array.

Drew, please look at my example above... This should work but for some reason "catDepth" filter returns nothing. The field exists and is output as "1" with perch:showall in category template. This should work right??

Drew McLellan

Drew McLellan 2638 points
Perch Support

What does debug give you in that case?

Drew McLellan said:

What does debug give you in that case?

Here is my code:

    $opts = [
        'filter'=>'catDepth',
        'match'=>'eq',
        'value'=>1
    ];
    perch_categories($opts);

Here is the debug output:

Debug Message SELECT * FROM perch2_pages WHERE pagePath='/test.php' LIMIT 1 SELECT setID FROM perch2_category_sets WHERE setSlug='' LIMIT 1 SELECT main.* FROM perch2_categories main WHERE 1=1 AND (catDepth=1) ORDER BY catTreePosition ASC Invalid query: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'catDepth' in 'where clause' Using template: \templates\categories\category.html

The same function without $opts outputs:

One

Two

    Three <== sub-item of Two

four

five

six

Debug Message
SELECT * FROM perch2_pages WHERE pagePath='/test.php' LIMIT 1
SELECT setID FROM perch2_category_sets WHERE setSlug='' LIMIT 1
SELECT main.* FROM perch2_categories main WHERE 1=1 ORDER BY catTreePosition ASC
Using template: \templates\categories\category.html

Perch Runway: 2.8.13, PHP: 5.6.8, MySQL: mysqlnd 5.0.11-dev - 20120503 - $Id: 3c688b6bbc30d36af3ac34fdd4b7b5b787fe5555 $, with PDO Server OS: WINNT, apache2handler Installed apps: content (2.8.13), assets (2.8.13), categories (2.8.13), collection_2 (2.8.13), perch_events (1.9.2), perch_forms (1.8.3), perch_gallery (2.8.6), perch_shop_paypal (1.2.3), perch_members (1.3), ketterus_rks (2.0) App runtimes: <?php $apps_list = array( 'content', 'categories', 'perch_events', 'perch_forms', 'perch_gallery', 'perch_members', 'perch_shop_paypal', 'ketterus_rks', ); PERCH_LOGINPATH: /perch PERCH_PATH: G:\Web Hosting\Websites\go2rks\public_html\perch PERCH_CORE: G:\Web Hosting\Websites\go2rks\public_html\perch\core PERCH_RESFILEPATH: G:\Web Hosting\Websites\go2rks\public_html\perch\resources Image manipulation: GD PHP limits: Max upload 2000M, Max POST 8M, Memory: 128M, Total max file upload: 8M Resource folder writeable: Yes HTTP_HOST: local.go2rks DOCUMENT_ROOT: G:/Web Hosting/Websites/go2rks/public_html REQUEST_URI: /perch/core/settings/diagnostics/ SCRIPT_NAME: /perch/core/settings/diagnostics/index.php

Hello, I'm just wondering if something is happening with this ticket?

Robert Ketter

Robert Ketter said:

Hello, I'm just wondering if something is happening with this ticket?

Robert Ketter

Hi there, the original question here is closest to what I am wondering at the moment.

I'd like to display only the parent category (via post_in_list.html) for each post on my main blog index page. I still want to have sub-categories available within the actual posts and for archives.

This is what I have now and it displays both parent and sub-categories on my blog index page:

<perch:categories id="categories" set="blog"> <a href="archive.php?cat=<perch:category id="catSlug" type="slug" />" class="p-category"> <perch:category id="catTitle" type="text" /> </a> </perch:categories>

Any help would be greatly appreciated!

Olympia,

Please move your question to a new thread. This thread is old, and therefore it's not monitored like a new thread would be. Also, while your problem may seem the same, it may be totally different and therefore should be in a separate thread.

Thanks so much. :)