Jump to content

Getting a page title from urlSegment


Marty Walker
 Share

Recommended Posts

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

?>
Link to comment
Share on other sites

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

  • Recently Browsing   0 members

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