We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Using category group in URLs - extracting this variable is proving tricky...

Development and Programming

joelene's avatar
joelene
87 posts
17 years ago
joelene's avatar joelene

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

http://www.my-site.com/categories/the-category-name/the-entry/

I would like

http://www.my-site.com/categories/the-category-group-name/the-category-name/the-entry/

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.

       
Jesse M's avatar
Jesse M
92 posts
17 years ago
Jesse M's avatar Jesse M

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.

e.g.

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

...

{/exp:weblog:entries}
  • 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 --}

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

...

{/exp:weblog:entries}

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}"}

...

{/exp:weblog:entries}

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: {segment_1}

Template / category group: {segment_2}

Category: {segment_3}

URL title: {segment_4}




/*

In plugin code

($IN is a global variable)

*/

$template_group = $IN->fetch_uri_segment( 1 );

$template = $IN->fetch_uri_segment( 2 );

$category = $IN->fetch_uri_segment( 3 );

$url_title = $IN->fetch_uri_segment( 4 );

Hope this helps.

       
joelene's avatar
joelene
87 posts
17 years ago
joelene's avatar joelene

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

www.mysite.com/the-parent-category-group/the-category-name/the-url-title

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

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.