EE: Low Seg2Cat extension

This ExpressionEngine extension will make it easier to work with categories. This post now provides a place for support for the Low Seg2Cat extension.

Download and docs for Low Seg2Cat can be found in the Software section

November 4th 2008 | Add-Ons, Categories, ExpressionEngine, Segments | 82 comments

Comments

  1. 1 Peter November 4th 2008, 18:29

    Hi Low

    Looks nice, we’ve always handled this with PHP as you can imagine, too bad this didn’t come out two weeks sooner, we could have used this in a project that is nearly finished.

    Thanks for your work, will definitely consider this in future projects.

  2. 2 Ryan Masuga November 4th 2008, 18:36

    Low, I’ve always used PHP on my templates to get the category ID from a segment in the URL, so this looks good to me. I think I may be able to use this on my current project. Also adding this to the devot:ee (devot-ee.com) add-on library.

  3. 3 Erwin Heiser November 5th 2008, 14:34

    I must admit I underuse categories, but this looks seriously sweet :)

  4. 4 Zac November 10th 2008, 00:11

    Nice addition to the EE plugin world! I definitely have a use for this on a site I’m working on now. Thanks!

  5. 5 Don Ambridge November 13th 2008, 23:49

    Brilliant. Love you.

  6. 6 Joerg January 2nd 2009, 11:50

    Is there a chance to get the {segment_n_category_url_title} ?
    Would be awesome!

  7. 7 Joerg January 2nd 2009, 21:38

    I just figured it out myself > I haven’t got a clue what I’m doing - but that seems to work:
    add this line after line 106:

    $data[’segment_’.$ids[$row[’cat_url_title’]].’_category_url_title’] = $row[’cat_url_title’];

    This now works fantastically well for breadcrumbs etc ….

  8. 8 Low January 3rd 2009, 11:03

    Joerg, that hack isn’t even necessary. This extension checks the url segments and compares them to the category_url_titles. So, if a category is found, then the segment equals the category_url_title.

    In other words, {segment_n} == {segment_n_category_url_title} :-)

  9. 9 Stanislav Majerski January 13th 2009, 18:28

    Just started using this plugin. Great help. As noted sometimes is nice to get rid of the CATEGORY word in the URL.

    Thank you

  10. 10 Tristan Bailey February 27th 2009, 03:54

    This looks quite nice and will save the need for category in the url which I don’t like. Will give it a whirl soon.

  11. 11 Tory April 7th 2009, 22:41

    How is it used?? Can someone please post an example so I know how to use it?

    Thanks!

  12. 12 Stan April 8th 2009, 11:45

    Tory. See if this example will clarify:

    URL: example.com/animals


    {exp:weblog:entries weblog="weblog_name" category="{segment_1_category_id}" }
    List only entries that are assigned to the category named animals (in Category URL Title)
    {/exp:weblog:entries}

    This helps to create cleaner URLs and get rid of the “category” or “c” word from the url.

  13. 13 Tory April 8th 2009, 13:49

    Thanks for the example, appreciate it. But…

    It seems to work perfect on some categories, but not at all on a few. Seems that if the category id is more than one word (for example, I have a category that has two words and the url has a hyphen separating the words) it’s not working.

    I tried using this:

    {exp:weblog:entries weblog=”portfolio” category=”{segment_3_category_url_title}” }

    But it’s not working either. Any help would be greatly appreciated.

    Thanks.

  14. 14 Low April 8th 2009, 13:57

    Tory, {segment_3_category_url_title} is not available, since it would be exactly the same as {segment_3} and therefore pointless. The category parameter in the weblog:entires tag expects an ID, so you’d have to use {segment_3_category_id}. And you should probably also add dynamic="off" to the weblog:entries tag.

  15. 15 vani May 12th 2009, 21:20

    Hi - Is there an example on how this extension would be used?

  16. 16 Low May 12th 2009, 21:43

    Vani, you can use the variables made available with this extension in several ways. There is no one single purpose other than having the said variables available. So, if there’s a category url_title in the url, you could use <title>{segment_3_category_name}</title> as your page title or {exp:weblog:entries weblog="my_blog" category="{segment_2_category_id}" dynamic="off"} to get all entries from the given category without the use of the Category URL Indicator.

    In short, just read the ‘What does it do’ bit and then ask yourself: can you think of an example on how this extension could be used?

  17. 17 Nathan Pitman May 19th 2009, 22:44

    Hi there, will this work with gallery categories, or would it be possible to add a parameter to specify that it does? :)

  18. 18 Low May 19th 2009, 22:59

    Hey Nathan. I’m afraid Gallery categories aren’t supported. Yet. :)

  19. 19 Todd May 28th 2009, 12:52

    Thanks Lodewijk (how do you pronounce that?) for your excellent work here. I will definitely be using this beautiful solution in the future.

  20. 20 pirco June 3rd 2009, 04:44

    hi, can you list requirements? I get

    “MySQL ERROR:

    Error Number: 1146

    Description: Table ‘OUexpEng.EE__extensions’ doesn’t exist”

    thanks

  21. 21 pirco June 3rd 2009, 18:14

    is it possible that this extension won’t run in EE 1.6.7?
    trying to enable it, I get the error above.

  22. 22 Low June 3rd 2009, 21:28

    @pirco: as far as I know, this extension works with the latest EE build. Do other extensions work?

  23. 23 pirco June 3rd 2009, 21:31

    yes.
    I’m on PHP5, btw.

  24. 24 Low June 3rd 2009, 21:45

    Pirco, can you try a fresh copy? I made a small change — see how that works out.

  25. 25 pirco June 3rd 2009, 22:32

    yup, that works!
    thanks for the personal attention. much appreciated.

  26. 26 earph June 4th 2009, 23:08

    I’ve seen the many examples in the comments, but I’m still having a hard time wrapping my brain around how to implement this.

    Ideally, I would like my URL to look like: http://www.domain.com/category/movie-title/

    I appreciate any other advice you could give. Thanks!

  27. 27 Mark @ Alchemy United June 5th 2009, 03:08

    WOW! Sweet! If this works then I think you just saved my life. No need for /category/. No need to pass Cat_IDs. Thank you for reading my mind.

    I have a question… (And it might be pre-mature since I haven’t read eveything nor even installed this yet) What happens if the segment is an entry URL? Or is it assumed that dynamic=”off”?

  28. 28 Low June 5th 2009, 08:01

    @earph and @Mark: setting up URL structures is totally up to you. In EE, you can set up any kind of URL structure you want, using the {path="seg1/seg2"} variable.

    This extension does not build URLs for you, it merely looks at the current URI and fetches matching categories, which you can then use in your templates. This means that any match with a category url title is available. If the segment is an entry url title, and it doesn’t match a category url title, then the {segment_n_category_...} variables will be empty. You get the variables, you decide how to work with them.

    This means, earph, that you could use the URL structure you want, but I can see a problem there. The first segment is usually used to identify the template group. This is good for keeping your templates structured and catching 404’s. In your case, the first segment is a dynamic value, which you’d have to read and manage in the index template of your default template group, which would be a challenge. You could also create as many template groups as there are categories, but that’s not something I’d recommend.

  29. 29 earph June 5th 2009, 17:52

    Thanks for the response low!

    I managed to mix your extension in with the method found here: http://fortysevenmedia.com/blog/archives/setting_up_custom_category_url_structures_in_expressionengine/

    It did allow me to set up the path exactly as I request, and you’re right IT IS a dynamic value. I hope it doesn’t make things too complicated down the road. Do you think I’ll have a problem using the method above? It seems like it’s handling the dynamic value really well.

  30. 30 Mark @ Alchemy United June 5th 2009, 20:50

    Hey Low. Two more questions, if you don’t mind:

    1) “If no matching category is found for a segment, an empty string is returned.” So the global is still created for the segment for the value is ” “, correct?

    2) Does this work with Multi-Site Manager (MSM)?

    This plugin looks great for how we’ve been approaching EE based sites. Perfect even! Now can someone tell me how to get rid of segment_1 so I can do mysite.com/category-A instead of mysite.com/some-template-folder/category-A? Basically I want my “root” / core pages to appear there and then use template folders for particular areas (e.g. blog, freelance, etc.)

    Thanks again. I

  31. 31 mahalie June 11th 2009, 21:39

    Almost done with a new section of my site using this extension but am having difficulties with pagination, even using paginate_base - it just loops!

    Has anyone figured out pagination use with low seg3cat?

  32. 32 Low June 12th 2009, 07:33

    @Mark: 1: Correct. 2: Yes.

    You’ll need to set up some logic in the index template of your default template group. Try using embeds. Something like this:

    {if segment_1_category_id != ''}{embed="group/template_1"}{/if}
    {if segment_1_category_id == ''}{embed="group/template_2"}{/if}

    @mahalie: Dynamic pagination will be flaky if you use this approach. One way to work around this, is using the offset="" parameter and setting its value in another segment. You could end up with URIs like this: /group/category1/50/ and use this weblog:entries tag: {exp:weblog:entries category="{segment_2_category_id}" offset="{segment_3}" dynamic="off"}. You’d have to calculate the offset value yourself, though.

  33. 33 Peter June 12th 2009, 07:42

    We’ve implemented a multi-level hierarchical navigation by using only categories lately on a client site, for pagination, we had to use the dynamic="off" approach by applying a small hack to the EE core: http://expressionengine.com/wiki/Enable_pagination_with_dynamic_off

  34. 34 Steve Abraham June 19th 2009, 15:05

    Hi Lodewijk,

    Would it be possible to update this so it accepts site=”site_short_name”, so on an MSM environment you can specify which site the category group belongs to?

    Cheers!
    Steve.

  35. 35 Steve June 24th 2009, 17:01

    Can’t seem to get the category name, where the category has two words. Using dynamic=’off’. I see someone else mentioned this earlier. Is there a bug here? Or is there a fix?

  36. 36 Steve June 24th 2009, 17:11

    Forget that last query - my template wasn’t updating for some reason. No bug :)

  37. 37 Low June 24th 2009, 17:11

    @Steve: what are you trying to do? I got the feeling it’s not this extension you’re having trouble with, but rather with the thing you’re trying to accomplish.

    @Steve Abraham: this extension already is MSM-friendly — it only checks the categories belonging to the site you’re viewing.

  38. 38 Mark @ Alchemy United June 24th 2009, 22:53

    Hello again Low…

    Ok, still sounds great. I’m really anxious to try this. BUT… could you be so kind as to look at this EE forum topic I have going and offer your comments?

    It would help a lot. Thx man.

    http://expressionengine.com/forums/viewthread/121037/#599638

  39. 39 Mark @ Alchemy United June 24th 2009, 22:56

    Low…

    One more Q… What about category custom fields? Do you global those as well?

  40. 40 Low June 25th 2009, 11:51

    @Mark: if you’re having trouble with your site, please ask the EllisLab support crew — it’s what they’re there for. :) Seg2Cat does not include custom fields, because I wanted to make it simple and lightweight. You can use PHP or the Query module instead.

  41. 41 Mark @ Alchemy United July 13th 2009, 18:48

    Hello again Low!

    First, used your extension to “convert” a site I was about to take live. Brilliant! Gotta love those cleaner URLs

    With regards to my other request (EE forum), it was related to multi-site manager, maybe this extension, and trying to provide the idea solution to the client. It wasn’t “tech support” as much as EE theory.

    Thanks again. As I use this going forward I’m going to have to tack on a fee to my invoices to kick back to you :) Cheers!

  42. 42 notacouch July 20th 2009, 23:15

    Hi Low

    I seem to have encountered a problem, don’t know if this was reported or not, but if there are multiple segments that are the same only one will get parsed out e.g.
    /template_group/category_url_title/entry_title
    where seg 2 and 3 are the same, 2 will not get assigned.

    Made my own fix for it

    $ids = array(); before the first foreach loop then inside after the data

    $escaped_seg = $DB->escape_str($seg);
    if( isset( $segs ) && in_array($escaped_seg, $segs) )
    {
    if( ! isset( $duplicates ) ) $duplicates = array();
    if( ! isset( $duplicates[$escaped_seg] ) ) $duplicates[$escaped_seg] = array( $ids[$escaped_seg] );
    $duplicates[$escaped_seg][] = $nr;
    }
    //
    $ids[$escaped_seg] = $nr;
    $segs[] = $DB->escape_str($escaped_seg);

    then later.. just to give you the idea

    // overwrite values in data array
    $iter = 0;
    $iter_end = 1;
    $temp_ids = array( 0 => $ids[$row['cat_url_title']] );
    if( isset ( $duplicates[$row['cat_url_title']] ) )
    {
    $iter_end = count($duplicates[$row['cat_url_title']]);
    $temp_ids = $duplicates[$row['cat_url_title']];
    }
    for($iter; $iter < $iter_end; ++$iter)
    {
    $data['segment_'.$temp_ids[$iter].'_category_id'] = $row['cat_id'];
    }

    it got the job done =)

  43. 43 Benek July 28th 2009, 05:32

    Awesome extension. Just what I need and saved me a top of time figuring out how to do this manually. Thanks!

  44. 44 heaversm July 28th 2009, 13:06

    lifesaver - thanks!

  45. 45 Boyink September 10th 2009, 20:11

    Any chance this could also return parent category information? I’m doing some menu work and need to know the ID of the parent category of the current category so I can set the menu state correctly.

  46. 46 Low September 11th 2009, 14:06

    Hey Mike. Sure, that’s actually quite an easy addition. As soon as I get Git working on Snow Leopard again, I’ll update the extension. Or just send me an email if you need it straight away.

  47. 47 Boyink September 11th 2009, 15:32

    Thanks!

  48. 48 3Easy October 19th 2009, 15:18

    Low,

    The deeper one delves into EE, the greater the love of this community. About to code up something along these lines, I realise I don’t have to because the hard work has been done. Thank you, love you though we’ve never met.

    Muchos,
    3Easy.

  49. 49 akc October 31st 2009, 00:03

    This is extremely useful, and I’m thrilled that it’s available to use it on my current project. It’s perfectly handy in creating a tagging system, using EE’s categories, with clean URLs like http://somesite.com/tag/tagname for pages that show all the entries in a particular tag (category). Thanks! (And thanks to Boyink! for referring me to it in the forum.)

  50. 50 Lee December 9th 2009, 11:46

    “If no matching category is found for a segment, an empty string is returned.”

    In EE2 I’m not finding this to be the case, for example if segment 4 of a url is not in use, the raw text {segment_4_category_name} is returned and not an empty string.

    Super useful code - many thanks.

  51. 51 Low December 9th 2009, 11:53

    Lee, if there is no {segment_4}, then {segment_4_category…} variables will not be set in the first place. In those cases, it’s better to do a check with a conditional: {if segment_4} [...] {segment_4_category...} [...] {/if}

  52. 52 Paradise February 1st 2010, 18:45

    Hi,

    it doesn’t work in the new EE 2.

  53. 53 Low February 1st 2010, 19:05

    Paradise, version 2.2 does.

  54. 54 Paradise February 3rd 2010, 09:06

    Hi Low,

    i have the same proplem with Low Replace. If i go to Extensions or Plugins the github site shows up. On Seg2Cat i get this php errors at the bottom.

    Message: Class Low_seg2cat_ext does not exist in (/mypath/expressionengine/third_party/low_seg2cat/ext.low_seg2cat.php)
    Filename: cp/addons_extensions.php
    Line Number: 121

    and

    Message: Cannot modify header information - headers already sent by (output started at /mypath/expressionengine/third_party/low_seg2cat/ext.low_seg2cat.php:15)
    Filename: core/Common.php
    Line Number: 421

    Another question is - does now everything (plugin, ex, module go in the third_party folder?

  55. 55 Low February 3rd 2010, 09:15

    Paradise, EE2 works with 3rd party packages. That means everything is put into the third_party folder, in its own directory. By the look of your paths, this is the case.

    I cannot replicate your error. What 2.0 version are you using? The free trial, the freelancer or the (non)commercial version? Also, are there any other add-ons installed?

  56. 56 Paradise February 3rd 2010, 09:36

    They are the only add-ons. You know i truncated the path :-)? The full path is

    /var/www/work.mydomainCOM/htdocs/something.de/system/expressionengine/third_party/low_seg2cat/ext.low_seg2cat.php

    Its v2.0.1pb01 and a free ride :-)

  57. 57 Low February 3rd 2010, 10:03

    Paradise, still cannot reproduce. Can you install other add-ons without problems?

  58. 58 Paradise February 3rd 2010, 12:17

    Ok, for a test i took Ryan Irelan’s “First Timer” extension and it works…

  59. 59 Low February 3rd 2010, 13:25

    Looks like to me you’re trying to install version 1.x instead of the EE2 compatible version v2.2 (grab it at github).

  60. 60 Paradise February 3rd 2010, 14:25

    Downloaded it again (what i did 3 times before) and the CP allways shows the github site. I found a thread in the EE forum where somebody had the same problem with another add-on.

    Can i not post links here?

  61. 61 Low February 3rd 2010, 14:41

    You can post links here. But maybe something is going wrong with the download from GitHub. I’ve zipped up a version you can download here: Low Seg2Cat v2.2.

    Be sure to delete the other versions you downloaded.

  62. 62 Paradise February 3rd 2010, 15:01

    Cool it works. So now i need also Low Replace :-)

    Seems to be a github problem, cause the guy in the forum had the same problem with another add-on from github.

  63. 63 Low February 3rd 2010, 15:16
  64. 64 Paradise February 3rd 2010, 15:28

    Thanks Low for your time/help. Now everything works. I’m to stuppid for github? Just right click and save - or?

  65. 65 Low February 3rd 2010, 15:32

    To download from GitHub, just click the ‘Download Source’ button on the top right of the project page, below the search form and main menu.

  66. 66 daulex February 8th 2010, 17:35

    first of all, thank you for the plugin.

    question:
    {if "{segment_2_category_id}" == "" OR "{segment_2_category_id}" == "{segment_2_category_id}"} aaaa {if:else}bbb{/if}

    doesn’t work even though {segment_2_category_id} is returned when there is no segment 2.

    Is there a workaround? Why can’t it just be “” (nothing) instead of echoing the name of the function?

  67. 67 Low February 8th 2010, 17:47

    Daulex, what are you trying to accomplish? By the looks of it, something like this might be more appropriate:

    {if segment_2}{if segment_2_category_id}aaa{if:else}bbb{/if}{/if}

    But beware — using this code as input for another tag will not work, because of the parse order.

  68. 68 daulex February 8th 2010, 17:57

    Thank you for the swift reply.

    That is pretty simple. I have yet to test it, but that’ll work.

    The question remains, any chance the plugin could be improved to return a blank when the segment is not present?

  69. 69 daulex February 8th 2010, 18:02

    I’m simply trying to pull the appropriate category based on the url segment. While pagination available at any stage.

    I reworked the code you provided to:
    {if segment_2}{if segment_2_category_id}{segment_2_category_id}{if:else}{segment_1_category_id}{/if}{/if}

    I’m getting this error Invalid EE Conditional Variable: {if segment_2_category_id}, which is what you probably warned me about.

    Thank you for taking the time to help me man.

  70. 70 daulex February 8th 2010, 18:09

    Sorry for what’s looking like spam now, that only appears if there is no segment_2, otherwise works as expected, until I put it where I need it.

    How’s this for a line of code:
    {exp:channel:entries channel="products" limit="1" paginate="bottom" category="{if segment_2}{if segment_2_category_id}{segment_2_category_id}{if:else}{segment_1_category_id}{/if}{/if}"}

  71. 71 Low February 8th 2010, 18:15

    Daulex, putting an advanced conditional (the one with {if:else}) inside a parameter will not work. Make sure you know what the value is of the variable before using it as a parameter value.

    So, try this instead:

    {if segment_2}{preload_replace:the_category="{segment_2_category_id}"}{/if}
    {if segment_1}{preload_replace:the_category="{segment_1_category_id}"}{/if}
    {preload_replace:the_category=""}
    {exp:channel:entries category="{the_category}"}
    ...
    {/exp:channel:entries}

  72. 72 daulex February 8th 2010, 18:38

    Dude, I love you.

    That works… until I start using pagination :D

    site.com/cat/ works
    site.com/cat/P0 - breaks the whole thing as the_category goes blank :( which is why I was going to rely on using segment_2_category_id where you are now using if segment_2 in the first place.

    I’m a wordpress heavyweight and really can’t believe that something that is so simple in wordpress is such a pain in ee. Really appreciate your time man.

  73. 73 Low February 8th 2010, 18:49

    Then there’s just one option, really. Use PHP on input in your template. That way you can check the value of the segments and set variables accordingly.

    But since that has little to do with the Seg2Cat extension, I suggest you check out the EE Forums for help on that.

  74. 74 daulex February 9th 2010, 10:20

    Thank’s Low, I understand the way it should work, however the only reason I asked you for help was the fact that your plugin echoes it’s own function call when there is no segment, which is extremely confusing. I just want to know if that can be avoided, as there is no way check against it.

    Something as logical as
    {if "{segment_2_category_id}" == "" OR "{segment_2_category_id}" == "{segment_2_category_id}"}

    Does not work. Which is why I asked you if the plugin can be improved.

  75. 75 Low February 9th 2010, 10:25

    Slight difference, the variable isn’t replaced if the segment isn’t present. If it should be replaced, then I need to set all variables for each possible segment (I believe 9 is the max at the moment). So, that means setting 9 x 5 = 45 variables, even when there’s no need.

    I’ll need to look into performance before I make such changes, but your feature request is noted.

  76. 76 Paradise February 16th 2010, 07:12

    Hi,

    how about a {last_segment_category_id}.
    Maybee it can be done with the {last_segment} motification from the path.php.

    I need to get the id from the last segment into the Channel/Weblog Entries Tag and don’t know how.

  77. 77 Low February 16th 2010, 10:52

    Paradise, I’ve updated the extension. There’s a new setting ‘Set all segments’, which will initiate all 9 possible segments. That should cater to your needs.

  78. 78 Brendan February 19th 2010, 10:19

    Hi Low

    What’s the chance of getting {category_n_group_name} as well?

    Plugin is pure gold by the way.

  79. 79 Low February 19th 2010, 10:28

    Hey Brendan,

    Those chances are slim. I want to keep this extension as lightweight as possible, by only querying one table once. Adding the group name will require joining another table, which won’t be necessary in most cases.

    You could use the query module, though. Something like this should do it:

    {exp:query sql="SELECT group_name FROM exp_category_groups WHERE group_id = '{segment_n_category_group_id}'"} {group_name} {/exp:query}

  80. 80 Brendan February 19th 2010, 13:34

    Thanks Low, worked a treat.

  81. 81 Mike February 20th 2010, 22:14

    Hi - this doesn’t appear to be working for me - I’ve downloaded v1.3 and installed the language and extension files to the proper directories, but nothing shows up in my extensions list. When I attempt to run a trace (ex: {segment_4_category_id}) within the weblog entries tag, the tag reads as its literal interpretation, and nothing is rendered. Any ideas? I’m using expression 1.6.8

  82. 82 Johan Baaij March 6th 2010, 16:16

    I get this in my extensions manager:

    No direct script access allowed

    What could be wrong :(?

Leave a comment



Some html (a, em, strong, etc) allowed.
Email won’t be displayed and takes care of your Gravatar.