Jump to content

Recommended Posts

Posted

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

Posted

Just guessing, but could it be that $urlcat is actually PageArray? Try $urlcat->first()->title instead (or change that "find" to "get".)

  • Like 2
Posted

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 :)

  • Like 2
Posted

make sure you sanitize that value before using it in the selector and throw a 404 if the page is not found.

  • Like 1
Posted

@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>";
      }

?>
Posted

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>";
  • Like 2
Posted

Get doesn't build the array in PHP, it just gets the requested page from the database and creates only that object.

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...