Marty Walker Posted September 26, 2013 Posted September 26, 2013 Hi, I'm working on a simple products site and I'm using url segments to filter the products by category. There's no problems getting the products that way. What I'm trying to do is get the category title for the url I'm on. eg: /products/19th-century <- urlSegment <?php if($input->urlSegment1) { $urlcat = $pages->get("/categories/")->find("name=$input->urlSegment1"); echo "<h3>Category: <i>" . $urlcat . "</i></h3>"; } ?> So $urlcat gets me the category ID. How would I go about getting the title of the category? I've tried $urlcat->title to no avail. Cheers Marty
teppo Posted September 26, 2013 Posted September 26, 2013 Just guessing, but could it be that $urlcat is actually PageArray? Try $urlcat->first()->title instead (or change that "find" to "get".) 2
diogo Posted September 26, 2013 Posted September 26, 2013 You are using a find to get the page. That gives you a page array instead of the page object. for trying, $urlcat->first()->title should do the job, but you should change your find to a get. (on mobile) edit: teppo, not fair, I'm in disadvantage here 2
Marty Walker Posted September 26, 2013 Author Posted September 26, 2013 Thanks chaps. It was indeed a page array. Silly me.
diogo Posted September 26, 2013 Posted September 26, 2013 make sure you sanitize that value before using it in the selector and throw a 404 if the page is not found. 1
Marty Walker Posted September 26, 2013 Author Posted September 26, 2013 @diogo. Yes I had a thought to do that. Here it is for anyone else who might need something similar. <?php if($input->urlSegment1) { $name = $sanitizer->pageName($input->urlSegment1); $urlcat = $pages->get("/categories/")->find("name=$name"); echo "<h3>Category: <i>" . $urlcat->first()->title . "</i></h3>"; } ?>
diogo Posted September 26, 2013 Posted September 26, 2013 At the computer now. It would be more efficient if you would do this instead: $urlcat = $pages->get("/categories/")->get("name=$name"); // <- get instead of find echo "<h3>Category: <i>" . $urlcat->title . "</i></h3>"; or this: $urlcat = $pages->get("/categories/$name"); echo "<h3>Category: <i>" . $urlcat->title . "</i></h3>"; 2
Marty Walker Posted September 26, 2013 Author Posted September 26, 2013 OK. Thanks for that. So get is being more 'direct' than find?
diogo Posted September 26, 2013 Posted September 26, 2013 Get doesn't build the array in PHP, it just gets the requested page from the database and creates only that object.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now