Jump to content

Filter pages by "category"


MilenKo
 Share

Recommended Posts

@fbg13 as I wrote earlier, for the /category/ url I use 'categories' template. For any child page inside the category (eg. /category/bakery) i use 'categories-list' which got confirmed by the $page->template->name.

The whole idea for the approach was to have the category template list all the categories that are just children pages of Home > Recipe Categories and have only a few fields - title, category_icon, category_image, category_description

So browsing to food.pw/category/ would list ONLY the categories with their images linked to the category url: Bakery would be food.pw/category/bakery.

As far as I just needed to resolve the name of the subcategory after the /category/ and use it to filter the recipe results to only those that have 'bakery' assigned as a category.

I think I can grab the url using $_SERVER['REQUEST_URI'] and trimming it a bit to get the last part. However I am not sure would this approach would allow the caching to be working properly so will experiment a bit and see what would happen.

I already tried to add this code to the url-segments.php (as per your earlier suggestion):

<?php 
    $segment = $input->urlSegment; //Just to test segments
    $url = $_SERVER['REQUEST_URI']; //Get the url segments after the FQDN
    $path = explode('/', $url); //Removing the backslashes from the url
    $cat = end($path); //Obtain the last part of the url as the category to filter
    
    echo "Segment: " . $segment . "<br />";
    echo "URL: " . $url . "<br />";
    echo "Catetory: " . $cat . "<br />";
?>

And as a result I got:

Segment: i-am-a-segment
URL: /urlsegments/i-am-a-segment
Catetory: i-am-a-segment

So even if I decide to have subcategories, I would still be able to resolve the last one and filter results by $pages->find('template=categories-list, recipe_category=$cat)

Brosing to: http://food.pw/urlsegments/i-am-a-segment/blabla/tralala

Shows as a result: 

Segment: i-am-a-segment
URL: /urlsegments/i-am-a-segment/blabla/tralala
Catetory: tralala

So what you think? Should I give it a try and test the cachig if it works ?

 

 

categories-listtegories-listcategories-list'

Link to comment
Share on other sites

You're complicating things too much. Since you don't wanna share a site profile this is my last guess.

I think when you go to food.pw/category/bakery/ you actually access an existing page bakery, which is a child of category, so there are actually no url segments.

Link to comment
Share on other sites

@fbg13 your assumption is correct and yes, there is a parent Category and a child page - Bakery. It is my misunderstanding of what the urlSegments are used for. After your suggested test and reading a bit I found out that there is nothing wrong with the setup and it seems that the urlSegments are not meant to work for an existing url.

As far as I just need to read the last segment of the url and filter the results of it, I think that should work the way I need it (not depending on the deepenes of subcategories level).

Let me try that and will share how it works.

Link to comment
Share on other sites

Guys, after a few tries, I got it fully working without the use of urlSegments. What I did was to grab the url and extract the last segment of it after removing the backslashes before and after and assigning the result to my category variable:

<?	$url = trim($_SERVER['REQUEST_URI'], '/');
	$path = explode ('/', $url);
	$cat = end($path);
	$results = $pages->find("template=recipes-inner, recipe_category={$cat}"); 
	foreach($results as $recipe) { ?> 
	..... your code .....
	<? } ?>

To simplify it a bit I was able to combine the first three lines into a fully working single one:

$cat = end(explode('/', trim($_SERVER['REQUEST_URI'], '/')));

but got an error in Tracy Debugger: PHP Strict standards: Only variables should be passed by reference - so had to split it to be code standards compliant.

Tested to add a subcategory inside a subcategory and it also worked fine as far as I assigned it to an existing recipe so I can jump to the next task of my plan.

Thanks  again to all of you guys, who guided me and helped to stay on the right path and keep on going. All the best!

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...