Jump to content

Category filter question


Bacelo
 Share

Recommended Posts

Hello,

I'm right now fighting to get content filtered by category. Maybe someone can help me...

I have set up a hidden page area as filter option:

- categories

  - cat 1

  - cat 2

  - cat 3

In some pages I'm able so select these categories as filters - work so far perfect.

But:

How can I filter the pages which have only the selected filters (categories)?

I use the following code as the categories should always be shown:

      <?php

    $categories = $pages->find("template=category");
	echo "<li>" .
    "<a href='{$page->url}'>All</a>" .
    "</li>";
    foreach($categories as $category) {
    echo "<li>" .
    "<a href='{$category->name}'>{$category->title}</a>" .
    "</li>";
    }
      ?>

That shows me all the categories and a click of a category should show the filtered content - works so far but:

If a category is clicked which has no pages -> no filter selected, all pages of the parent are shown instead of none.

Any idea?

Regards,
Bacelo

Link to comment
Share on other sites

Can you show the selector you use to get the pages that belong to one category? I'm not sure what your problem seems to be exactly.  It looks like your content pages have a page field to select one or more categories, in which case you can filter by that field like so, where "categories" is the name of the field and $category is the category, whether it's a page object or an ID:

$pages->find("categories~=$category");

If no page belongs to that category, nothing will be shown. You could also filter by the categories' titles like find("categories.title~=$string").

Also, you should probably use $category->url instead of $category->name in your category list.

Link to comment
Share on other sites

Jan, thank you for your reply and your help.

I tried your suggestions but with no success so far. I guess I'm still to new to PW and of course my skills too low... :wacko:

Regarding $category->name I used this instead of $category->url because of the url structure.

If I use $category->url I get "root/categories/category" which is wrong because it should be "root/parent/category"

Maybe I should show my site structure:

- Home

  - Parent 1

    - Child 1

    - Child 2

    ...

  - Parent 2

    - Child 1

    - Child 2

    ...

- Categories (hidden - not used as content - used as content-filter)

  - Category 1

  - Category 2

    ...

Configuration:

Template "category" for categories filter option (no file)

Field:

"category" of type "page"

-> Template of selectable page(s) "category"

-> Custom selector to find selectable pages "parent=/categories/"

Functionality:

- Admin: While adding a new child page I can select a checkbox-filter via field "category" (all categories show up)

- Page: I have a sub navigation for each parent which should show the categories - like a filter. If parent is opened, all childs are loaded. By selecting a category, only the filtered contend should show (url f.e. like parent 1/category).

This is working fine so far if content with that filter exists. If an category is empty, all content of the parent is shown.

So, either the category should be hidden as there are no pages with that category selected, or all categories are shown, but only filtered contend shows up at each category. Empty category = empty body.

Regards,
Bacelo

Link to comment
Share on other sites

I take it you're using UrlSegments for the categories in the parent template? The theory seems sound. There is probably some minor mistake in your parent template. If you post the relevant code that finds the child pages, we can have a look.

Link to comment
Share on other sites

This is the code in the template:

    <?php if(count($page->children("template=gallery, category=$input->urlSegment1")) > 0) : ?>

      <?php foreach($page->children("template=gallery, category=$input->urlSegment1") as $preview): ?>
        <div class="gallery">
          <?php if(count($preview->images)): ?>
            <?php $count=0; foreach($preview->images as $image): ?>
              <div class="thumb" rel="<?php print $preview->id; ?>">
              <?php
              $thumb = $image->height(320);
              ?>

Thank you Jan.

Link to comment
Share on other sites

Hi there,

I'm still trying hard to get this working but with no success... :(.

I'm trying both ways:

- filtered output for the navigation

- edit output in the template as suggested by Jan

Any ideas?

Thanks,

Bacelo

Link to comment
Share on other sites

I can't tell for sure what the problem is, so I think you should try to simplify things down and debug each variable to figure out where things are going wrong. Bring it back to this:
foreach($page->children("category=$input->urlSegment1") as $preview){
      echo $preview->title;
}

Things to check are that the name of the category field is correct. Remember this must be the name of the field that is used in the template on the current page and not the name of the parent page that houses the child categories.

Also try echo'ing $input->urlSegment1 to make sure it actually contains the name of the category you are looking for. If not you can firstly replace this with a hardcoded category name to make sure your selector is working, then track down why $input->urlSegment1 is not being populated.

Hope that helps to get you sorted.

  • Like 3
Link to comment
Share on other sites

Many thanks to both of you - by your help I was able to get it managed.

I also changed the output of my navigation so the empty categories even do not show up:

        foreach($pages->find("template=category") as $category) {
          if (count($lp->children("template=gallery, category=". $category))) {
            echo "<li><a href='{$category->url}'>{$category->title}</a></li>";
          }
        }

Many thanks again! :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...