nikola Posted September 14, 2012 Share Posted September 14, 2012 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 More sharing options...
diogo Posted September 14, 2012 Share Posted September 14, 2012 What happens if an article has multiple categories selected? That's for tags. Category should be only one. No? Link to comment Share on other sites More sharing options...
Soma Posted September 14, 2012 Share Posted September 14, 2012 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 More sharing options...
nikola Posted September 15, 2012 Author Share Posted September 15, 2012 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 More sharing options...
ryan Posted September 16, 2012 Share Posted September 16, 2012 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 More sharing options...
nikola Posted September 16, 2012 Author Share Posted September 16, 2012 Thanks Ryan, I'll also try ProcessPermalinks from Nico, might be solution. I'll have to add categories and subcategories to the module though... Link to comment Share on other sites More sharing options...
ryan Posted September 16, 2012 Share Posted September 16, 2012 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 More sharing options...
nikola Posted September 19, 2012 Author Share Posted September 19, 2012 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 More sharing options...
ryan Posted September 20, 2012 Share Posted September 20, 2012 I think that your code looks fine, but I'm not sure that I understand the question? Link to comment Share on other sites More sharing options...
nikola Posted September 21, 2012 Author Share Posted September 21, 2012 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 More sharing options...
nikola Posted September 22, 2012 Author Share Posted September 22, 2012 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 More sharing options...
ryan Posted September 24, 2012 Share Posted September 24, 2012 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 More sharing options...
nikola Posted September 25, 2012 Author Share Posted September 25, 2012 Ryan, thanks for the example, my code looks similar but I think this will solve the problem. Still I have to try it out, just have to finish some other things I'm working on. Will let you know how it goes. Thanks Link to comment Share on other sites More sharing options...
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