Jump to content

Append to URL


nikola
 Share

Recommended Posts

I have "Articles" page that hold all the articles. I have set up a page reference field where I can select different categories.

How can I accomplish this in url/slug:

Instead:

www.domain.com/articles/article-name

I'd like to have:

www.domain.com/category/article-name

Basically I need to strip out articles from the slug and add category that is selected in article itself through reference field.

What happens if an article has multiple categories selected?

Link to comment
Share on other sites

Let me search that for you:

There's some dozen more around about this subject, but I'm too lazy search. ;)

Multiple categories will also result in multiple urls, so setting canonical meta url might be a good idea. Depends what you want, but I don't see any issues with it if you handle it in some way.

Link to comment
Share on other sites

Sorry, I wasn't clear enough. I need to prepend category and subcategory name to url based on fields selected in article template. So if user selects in category field (page type) - category1 (they can select only one category per article) and in subcategory field multiple categories in which that article would suppose to appear for instance - subcategory1, subcategory2 it would prepend that selections to url stripping out articles container (page) from url.

Example:

Article - normal view url:

/articles/some-article

Article - virtual categories (strip /articles/, add /category/subcategory to url).

/category1/subcategory1/some-article

I know it would be confusing if article would have multiple subcategories selected but I've tried to do categories and subcategories with the tree approach but then when I would list articles it would show only those that are in certain category or subcategory. If user wants to have the same article in multiple categories then he would have to copy the same article to different subcategories. That way url's are working fine - the normal way but it's a problem if article belongs to let say 8 categories...

Can PW check what category and subcategory/ies are selected in the article and based on that it would display url as described above?

Thanks

Link to comment
Share on other sites

I would probably be worried about duplicate content with this approach. You could use the rel='canonical' meta like Soma mentioned, but it's still not ideal ... could be an unnecessary headache for redirects and future iterations of the site. Also might add some new challenges to statistics tracking. It's kind of going against the flow of how URLs work, but if you are okay with that you certainly could do it in ProcessWire. Since you are wanting to eliminate /articles/, which is a root level page, your homepage would have to be the one allowing URL segments and detecting when they are present. Your generation of the article URLs you asked about would go something like this:

function articleURL(Page $article) {
 return '/' . $article->category->name . '/' . $article->subcategory->name . '/' . $article->name . '/';
}
Link to comment
Share on other sites

If there are multiple categories/subcategories, would you just use the first one then? Like this:

$url = "/" . $article->categories->first()->name . '/' . $article->subcategories->first()->name . '/' . $article->name . '/'; 
Link to comment
Share on other sites

Hi Ryan,

how would I use such a function, for example:

$category1 = $pages->get("/categories/category1/");

$subcategory1 = $pages->get("/categories/subcategory1/");
$subcategory2 = $pages->get("/categories/subcategory2/");

and so on for multiple categories...

$articles = $pages->find("template=article, category=$category1, subcategory=$subcategory1|$cubcategory2, sort=-date");

foreach($articles as $article) {
echo "<a href='{$article->url}'>{$article->title}</a>";

}

Where would I execute it?

Link to comment
Share on other sites

Ryan, I use the code in this way, with URL segments turned on in the home template:


function articleURL(Page $article) {
 return '/' . $article->category->first()->name . '/' . $article->subcategory->first()->name . '/' . $article->name . '/';
}

$category1 = $pages->get("/categories/category1/");

$subcategory1 = $pages->get("/categories/subcategory1/");
$subcategory2 = $pages->get("/categories/subcategory2/");

$articles = $pages->find("template=article, category=$category1, subcategory=$subcategory1|$cubcategory2, sort=-date");

foreach($articles as $article) {
echo "<a href='" . articleURL($article) . "'>{$article->title}</a>";
}

The URL changes to exact URL that I need but doesn't show the article itself. It shows the code from the home template instead.

Link to comment
Share on other sites

I've also tried this code from WillC (modified it for my needs):

$pages->addHookAfter('Page::path', null, 'hookPagePath');
function hookPagePath(HookEvent $e) {
 $page = $e->object;
 if($page->template == 'article') $e->return = "/blog/$page->name/";
}

bu still get the same results.

I'm checking URL segments within home template.

Link to comment
Share on other sites

The URL changes to exact URL that I need but doesn't show the article itself. It shows the code from the home template instead.

It sounds like you've got the URL generation part down correctly, but your homepage template isn't detecting the URL segments correctly. Can you post the code you are using on your homepage template? If I understand your site structure correctly, you are going to want something like this:

if($input->urlSegment1) {

 $category = $pages->get('/categories/' . $input->urlSegment1); 
 if(!$category->id) throw new Wire404Exception();

 if($input->urlSegment2) {
   $subcategory = $category->child('name=' . $input->urlSegment2); 
   if(!$subcategory->id) throw new Wire404Exception(); 

   if($input->urlSegment3) {
       $article = $pages->get("parent=/articles/, category=$category, subcategory=$subcategory, name=$input->urlSegment3"); 
       if(!$article->id) throw new Wire404Exception();
       echo $article->render();

   } else {
       echo $subcategory->render(); 
   }

 } else {
   echo $category->render();
 }

} else {
 // render your homepage
}

Also double check that you are allowing URL segments on your homepage template (Setup > Templates > home > URLs > URL Segments.

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