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
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
Comments
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.
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.
I must admit I underuse categories, but this looks seriously sweet :)
Nice addition to the EE plugin world! I definitely have a use for this on a site I’m working on now. Thanks!
Brilliant. Love you.
Is there a chance to get the {segment_n_category_url_title} ?
Would be awesome!
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 ….
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} :-)
Just started using this plugin. Great help. As noted sometimes is nice to get rid of the CATEGORY word in the URL.
Thank you
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.
How is it used?? Can someone please post an example so I know how to use it?
Thanks!
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.
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.
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 adddynamic="off"to the weblog:entries tag.Hi - Is there an example on how this extension would be used?
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?
Hi there, will this work with gallery categories, or would it be possible to add a parameter to specify that it does? :)
Hey Nathan. I’m afraid Gallery categories aren’t supported. Yet. :)
Thanks Lodewijk (how do you pronounce that?) for your excellent work here. I will definitely be using this beautiful solution in the future.
hi, can you list requirements? I get
“MySQL ERROR:
Error Number: 1146
Description: Table ‘OUexpEng.EE__extensions’ doesn’t exist”
thanks
is it possible that this extension won’t run in EE 1.6.7?
trying to enable it, I get the error above.
@pirco: as far as I know, this extension works with the latest EE build. Do other extensions work?
yes.
I’m on PHP5, btw.
Pirco, can you try a fresh copy? I made a small change — see how that works out.
yup, that works!
thanks for the personal attention. much appreciated.
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!
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”?
@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.
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.
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
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?
@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.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_offHi 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.
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?
Forget that last query - my template wasn’t updating for some reason. No bug :)
@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.
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
Low…
One more Q… What about category custom fields? Do you global those as well?
@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.
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!
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 =)
Awesome extension. Just what I need and saved me a top of time figuring out how to do this manually. Thanks!
lifesaver - thanks!
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.
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.
Thanks!
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.
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.)
“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.
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}Hi,
it doesn’t work in the new EE 2.
Paradise, version 2.2 does.
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?
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?
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.phpIts v2.0.1pb01 and a free ride :-)
Paradise, still cannot reproduce. Can you install other add-ons without problems?
Ok, for a test i took Ryan Irelan’s “First Timer” extension and it works…
Looks like to me you’re trying to install version 1.x instead of the EE2 compatible version v2.2 (grab it at github).
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?
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.
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.
Low Replace v2.0
Thanks Low for your time/help. Now everything works. I’m to stuppid for github? Just right click and save - or?
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.
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?
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.
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?
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.
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}"}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}
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.
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.