EllisLab text mark
Advanced Search
Using category group in URLs - extracting this variable is proving tricky…
Posted: 18 June 2007 04:53 AM
Joined: 2006-02-01
114 posts

I want a more descriptive URL as I have categories that have the same name in different category groups and the default EE way is not really helping.

Instead of


I would like


Is there a way of grabbing the category group name and using the segments to stipulate more exactly where you are on the site?

I have name my template groups the same as the category groups so I can use the segments simply enough I just need to have a more descriptive URL as the same entries can pop up in different categories and I would like to be able to show which category the entry is being posted in.

Posted: 25 June 2007 12:55 AM   [ # 1 ]   [ Rating: 0 ]
Joined: 2007-05-03
106 posts

Hi joelene,

Sorry to see your post has gone unanswered for almost a week.

I had basically the exact same problem and I also had to override EE’s simple default URL format.  Originally I thought that was provided for by parameters of the {exp:weblog:entries} tag such as dynamic=“off”, category, entry_id, and url_title, but it has proved to be a pain in the neck.  I’ve done what I need to do, but it hasn’t been as straighforward as I anticipated.

The big problems I’ve encountered are:

* Uncertainty about the ability to supply parameter values for tags (e.g. {exp:weblog:entries}) using plugins, without which EE is only a fraction as usable (for a developer) as it could be.  I don’t understand why EllisLab doesn’t document the use of plugins for parameter values, but this thread seems to give it the official seal of “approval”.

* Behavior of {exp:weblog:entries} doesn’t always make sense (e.g. not tracking views, although I was told in that thread that that would be fixed in 1.6, more or less) when manually feeding it parameters (such as ‘url_title’).

I use URLs like /template_group/template/C10/url-title on my site.  I need to embed the category ID in the URL of each request to properly generate category sensitive navigation and other elements of the pages.

There are two techniques I’ve used:

* Supplying the values for {exp:weblog:entries} parameters using my plugin.


{exp:weblog:entries dynamic="off" url_title="{exp:my_plugin:get_url_title}" parse="inward"}



* Setting variables in path.php.  See Path.php Global Variables

To use this technique, you might do something like:

/* In path.php */

$request_URI explode"/"trim$_SERVER[ 'REQUEST_URI' ]"/" ) );

$global_vars[ 'my_category_id' $request_URI[2];

$global_vars[ 'my_url_title' $request_URI[3];

{!-- In your template --}

:weblog:entries dynamic="off" url_title="{my_url_title}"}



But if your requirements are that simple, you could probably skip setting the variables in path.php and do something like this:

{exp:weblog:entries dynamic="off" url_title="{segment_4}"}



So using your URL format (I’m not 100% what each segment in your URLs represents, so don’t take this too literally):

{!-- In your templates --}

Template group

category group{segment_2}


URL title


In plugin code

($IN is a global variable)


$template_group $IN->fetch_uri_segment);

$template $IN->fetch_uri_segment);

$category $IN->fetch_uri_segment);

$url_title $IN->fetch_uri_segment); 

Hope this helps.

Posted: 11 July 2007 11:30 AM   [ # 2 ]   [ Rating: 0 ]
Joined: 2006-02-01
114 posts

Hello - thank you very much for all that (please excuse my lack of a reply - I have been on holiday)
I have done it a slightly more convoluted route but it works quite well for me in that it mirrors the way multiple weblogs would work - as my parent categories dont change I have hard coded these in an array and used a query to find the category name and description from the entry_id which then writes the path into the urls.
The templates have

category=”<?php $query = $DB->query(“SELECT cat_id, cat_name FROM exp_categories WHERE parent_id = $my_category_id ORDER BY cat_name”);

  if ($query->num_rows > 0)
foreach($query->result as $row)
echo $row['cat_id']."|";

to generate a list of the entries for a specific parent id which in effect are the subcategories - it is more complicated that I have explained but if anyone really wanted to know then I can go into more detail - suffice to say it works and I can use the url format


One glorious day this will be the format of all categories and there will be much rejoicing