We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38290
    • 712 Posts
    A project I'm working on requires Categories across multiple Articles Containers, as well as 'global tag pages' that display tags across multiple Article Containers. I thought I'd share what I came up with. I'm not sure if it's the best way to go about this, but it seems to be working.

    To create URLs like /categories/modx and /tags/snippets, I created a custom page to handle 404s by setting the error_page System Setting. On that page, I call this snippet [[!errorPage]].

    $categoryURL = $modx->runSnippet('getCategoryByURL');
    $tagURL = $modx->runSnippet('getTagsByURL');
    if($categoryURL) {
    	return $modx->getChunk('feature-category-default',array('category' => $categoryURL));
    }
    if($tagURL) {
    	return $modx->getChunk('feature-tag-default',array('tag' => $tagURL));
    	
    }
    return $modx->getChunk('error-page');
    


    So I check for a category URL (/category/something) or a tag URL (/tags/something). If either is found, a call chunks and pass the category or tags slug to serve those pages appropriately. If neither is found, falls back to a default error-page chunk.

    I'm using getResources &where and &tvFilters to pull from multiple Article Containers and filter the results. Now I'm not able to use any of the nifty Article features on these pages.
            [[getResources?
            &parents=`6,8,9`
            &showHidden=`1`
            &limit=`15`
            &includeTVs=`1`
            &processTVs=`1`
            &tpl=`feature-all-item`
            &tvFilters=`postCategory==[[+category]]`
            ]]
    


    Here's what the URL sniffing snippets look like
    $url = $modx->runSnippet('curPageURL');
    $site_url = $modx->getOption('site_url');
    $u = parse_url($url);
    $folders = explode('/',$u['path']);
    
    return ($folders[1] == 'categories') ? urldecode(array_pop($folders)) : '';
    

    $url = $modx->runSnippet('curPageURL');
    $site_url = $modx->getOption('site_url');
    $u = parse_url($url);
    $folders = explode('/',$u['path']);
    
    return ($folders[1] == 'tags') ? urldecode(array_pop($folders)) : '';
    
    [ed. note: dinocorn last edited this post 11 years, 8 months ago.]
      jpdevries
      • 18367
      • 834 Posts
      Is the answer to simply not use Articles and create your blog and sections as originally described here http://rtfm.modx.com/display/revolution20/Creating+a+Blog+in+MODx+Revolution#CreatingaBloginMODxRevolution-CreatingtheSections

        Content Creator and Copywriter
        • Studying MODX in the desert - http://sottwell.com
          Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
          Join the Slack Community - http://modx.org
          • 37042
          • 384 Posts
          Any update on categories working natively within Articles?
          I have a few Articles blogs running for clients and it's consistently the most requested functionality.
          The category would also have to form part of the URL.
            ...
          • The URL stuff could be straightforward: you'd just need to make a plugin to fire on the 404 event and load up your dedicated categories page (I think the plugin solution is better than the above Snippet on the 404 page imo).

            If I had a budget, I could develop a serious MODX taxonomy add-on... it's overdue...
            • This may not be quite what you're after plus it's a bit of a hack, but here's a fairly simple idea for suping up Articles tags. I'll put it out here in case it's useful for somebody or if anybody wants to turn it into a more elegant solution.

              Articles stores a blog post's tags in a hidden TV called articlestags. It's just a comma-separated list of tags. You can create a new TV with an input type of Auto-Tag ( http://rtfm.modx.com/display/revolution20/Template+Variable+Input+Types#TemplateVariableInputTypes-AutoTag ). That gives folks a list of all the tags that've been used before: you can simply click the name to toggle them on/off, or type in new ones.

              Then create a basic plugin which on document save copies the tags from your new TV into articlestags. That way you get the benefits of the auto-tag TV, but tags/categories and their URLs work like they normally do in Articles; you don't have to redo anything.

              There's probably a way to hide the original tags field when you're editing a blog post so users don't type into there by mistake (anything you type in there just gets overwritten when you save the post, so nothing's going to blow up if they do). On one site I'm working on, I used Form Customization to create a tab just for Tags, so the auto-tag TV is at least more visible than it would be if it were grouped in with the other TVs in the Template Variables tags.

              Anyway, here's a quick and dirty plugin that copies the fancy tags into the normal tags. It's attached to the OnDocFormSave event.
              <?php
              
              if ($resource->get('parent') == '27') {  // if it's in our Articles container (resource ID 27)
              // the auto-tag TV I created has an ID of 34, so its contents are available via $_REQUEST['tv34']
              	$fancyTags = preg_replace('/,(\w)/', ', $1', $_REQUEST['tv34']); // add a trailing space after commas
              	if (!$resource->setTVValue(2, $fancyTags)) {
              		$modx->log(modX::LOG_LEVEL_ERROR, "Problem saving blog tags $fancyTags");
              	}
              }
              
              return;
              
                Extras :: pThumbResizerimageSlimsetPlaceholders
              • The way I took care of it was just to create for each category an Articles instance in the resource tree. This way the url also reflects the different categories and the admin can quickly see what articles belong to what category. The only drawback is that the user can't switch categories once the article has been created. One could use MIGXdb to create this kind of functionality. Here is a tutorial: http://rtfm.modx.com/display/ADDON/MIGXdb.Manage+Child-Resources+in+a+grid-TV+with+help+of+MIGXdb

                You would then need to integrate quip, archivist, getpage yourself however.
                  Benjamin Davis: American web designer living in Munich, Germany and a MODX Ambassador. I am also co-founder of SEDA.digital, a MODX Agency.
                  • 37042
                  • 384 Posts
                  Thanks sonicpunk.
                  I've got around it in the past by creating seperate Articles for each category too.
                  Would still love something 'baked in' though. Thanks for the suggestion.

                  Original request is a year old so hoping something can be done soon
                    ...
                    • 38118
                    • 8 Posts
                    I've added categories for articles fairly simply. I house all articles under a single resource that I keep hidden from menus. I then created a custom template variable called categories, and made it available to my articles display template. The categories tv has an input option of Listbox (Mulit-Select). This will allow you to put articles into multiple categories. In the option values (still on the input options tab of the tv), list all of your categories like:

                    Category 1||Category 2||Category 3

                    Then in the output options tab select Delimiter as your output option, and choose a delimeter character (I usually use a comma).

                    Then I create a container resource for each category, which will show up in the menu, and then make a getResources (or getPage) call to list articles, and use a tv filter to screen out articles of the category that I want. For example, lets say I have a category Video. I make a resource container called Video, and make a get resources call like the following on that page:

                    [[getResources?
                    &parents=`3`
                    &tpl=`articleTpl`
                    &showHidden=`1`
                    &includeContent=`1`
                    &includeTVs=`1`
                    &processTVs=`1`
                    &tvFilters=`categories==%Video%`
                    ]]

                    In the call, the &parents should be equal to the id of the resource that house all of my articles.

                    And it is important to note the &tvFilters parameter, where categories is what I named my TV to choose categories, and the % symbols surrounding the category name is important, because if you choose multiple categories for an article, this will output a delimited list of all the chosen categories. The % symbols acts as a wild card so that %Videos% will match the output of "Videos, Another Category, Category 3", for example.

                    So, now I have navigation built on my categories, and I can place a single article in multiple categories. You just have to be sure to choose your categories from the Template Variables tab when creating the articles.
                      • 38290
                      • 712 Posts
                      So a few of us have posted ways to "hack" Articles into supporting categories and tags, mostly using the 404 error page method. While this works, there are a problem I've run into that I'm curious if anyone else has any solutions for:

                      I've been able to wrap my getResources call with getPage, to add support for pagination of categories and tags pages. Problem is however, getPage is using the id of the resource (/404-page) rather than the current URL of the page (/categories/cats).

                      Is there a way to pass a base url to [[!+page.nav]] to manually set it?
                        jpdevries