Jump to content

[resolved] root URL and categories as page field


Sanyaissues
 Share

Recommended Posts

Hi, i'm working on a website with some articles and categories. The structure is something like this:

Home
+Articles

--First Article

--Second Article

+Categories

--Category1

--Category2

I'm using the categories page as a field to categorize the articles and create the sections of the general navigation (home / category1 / category2).

Currently the URL for a section looks like example.com/categories/category1/ and i want something like: example.com/category1/

Any Ideas? Thanks!

Link to comment
Share on other sites

Here's a solution that should work:

Enable urlSegments on the homepage template, then in the home-template you need some logic to handle the segments:

$home = true;
if ($input->urlSegment1) {
  $category = $sanitizer->selectorValue($input->urlSegment1);
  $c = $pages->get("name={$category}");
  if ($c->id) {
    echo $c->render();
    $home = false; 
  }
}

if ($home) {
  //Do your homepage stuff...
}

You need to create the links to the categories yourself.

  • Like 1
Link to comment
Share on other sites

One addition to the code Wanze provided: when using urlSegments it is highly recommended to throw 404 when urlSegment is invalid. So in that case:

$home = true;
if ($input->urlSegment1) {
  $category = $sanitizer->selectorValue($input->urlSegment1);
  $c = $pages->get("name={$category}");
  if ($c->id) {
    echo $c->render();
    $home = false; 
  } else {
    throw new Wire404Exception();
  }
}

if ($home) {
  //Do your homepage stuff...
}
  • Like 3
Link to comment
Share on other sites

  • 9 months later...

i am using blog profile and i want to remove categories section from url
 
+ Posts
   - post1
   - post2
 
+Categories
  - category1
  - category2
   
 
now it looks like blog/categories/category1
but i want it look like  blog/category1
 
how can i remove categories fro url. I try what says on this topic but i couldnt do it.

when i paste the apesia's code to home.php nothing happens.

still categories seen on category links.

help pls

Link to comment
Share on other sites

Make sure you have URL segments enabled on your "home" template. This is in Setup > Templates > edit: home > URLs. 

At the top of your home.php, you should then be able to have something like this:

<?php
if($input->urlSegmentStr) {
  $name = $sanitizer->pageName($input->urlSegmentStr);
  $category = $pages->get("/categories/$name/"); 
  if($category->id) {
    echo $category->render(); 
    return;
  } else {
    throw new Wire404Exception();
  }
} 

The other side of it is making sure that your category pages have their $page->path and $page->url calls represent the location you intend. You can do this with a hook. You'd want to add this in your /site/templates/blog.inc file. 

wire()->addHookAfter('Page::path', null, 'hookPagePath');
function hookPagePath($event) {
  $page = $event->object; 
  if($page->template != 'category') return;
  $event->return = "/$page->name/";
} 

Also for these examples, don't just copy and paste them. Go through line by line to understand how they work, as you may need to modify them specific to your case.

Link to comment
Share on other sites

  • 3 weeks later...

Thanks for your answer Ryan

I did exactly all the things you said.

Now category links looks without categories,

for ex:

before      /categories/category1/

now          /category1/

but when i click on the link /category1/ 404 page not found appears.

i am trying it locally using wamp server

http://localhost/blog  -> my home page

http://localhost/blog/category1    -> my category page

what might be wrong? is it related with urlSegment?

Link to comment
Share on other sites

  • 2 weeks later...

but when i click on the link /category1/ 404 page not found appears.

i am trying it locally using wamp server

 

It sounds like the second snippet of code is working, but not the first. Double check that URL segments are enabled in your "home" template settings. This is on the URLs tab. If that's not it, look at your PW version. Is it older that 2.3? If so, try replacing "urlSegmentStr" with "urlSegment1". Still not working? Add this at the top of your /site/templates/home.php file, temporarily, just to make sure it is working:

echo "<h1>urlSegmentStr: $input->urlSegmentStr</h1>"; 

What do you see?

Link to comment
Share on other sites

i see on the top  ->   urlSegmentStr: category1

i am using latest version i just downloaded it this month. and i also downloaded blog profile.

Is it possible to run it on your side and try to remove categories section from url? So if you success you can tell me the differences on the default blog profile. Or just send files so ican run it on my side.

Thanks

Link to comment
Share on other sites

  • 4 months later...

Make sure you have URL segments enabled on your "home" template. This is in Setup > Templates > edit: home > URLs. 

At the top of your home.php, you should then be able to have something like this:

<?php
if($input->urlSegmentStr) {
  $name = $sanitizer->pageName($input->urlSegmentStr);
  $category = $pages->get("/categories/$name/"); 
  if($category->id) {
    echo $category->render(); 
    return;
  } else {
    throw new Wire404Exception();
  }
} 

The other side of it is making sure that your category pages have their $page->path and $page->url calls represent the location you intend. You can do this with a hook. You'd want to add this in your /site/templates/blog.inc file. 

wire()->addHookAfter('Page::path', null, 'hookPagePath');
function hookPagePath($event) {
  $page = $event->object; 
  if($page->template != 'category') return;
  $event->return = "/$page->name/";
} 

Also for these examples, don't just copy and paste them. Go through line by line to understand how they work, as you may need to modify them specific to your case.

For me this works like expected, going to /category/ works. Unfortunately I can also access the category page by typing /categories/category/. Why doesn't /categories/category/ redirect to /category/?

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