We would like to implement a URL structure where category

We would like to implement a URL structure where category pages end in a trailing slash whereas end pages do not.

For example, if we had a site with a "mustang" category, the url would be /mustang/

If we had a page for the 2013 GT Convertible the URL would be /mustang/2013-gt-convertible

In summary, if you cannot drill down further, there is no trailing slash. If you can, there is a trailing slash.

Multiple developers have struggled to come up with a solution. Any suggestions would be appreciated.

  • Vaughan

    Hi @Ryan

    This could be possible I think.

    Using a function such as below in your theme functions.php

    function permalink_untrailingslashit($link) {
    	return untrailingslashit($link);
    }
    add_filter('page_link', 'permalink_untrailingslashit');
    add_filter('post_type_link', 'permalink_untrailingslashit');

    the hard part here is determining whether the permalink is for a category page or not.

    We could do a strpos on the $link But then we wouldn't know whether that was a category or a page

    /category/
    /this-is-a-page

    We may need to add code to the templates to determine this. which would require a bit more custom code. if your developers could get their head round the template part, to determine whether it's a cat page or not.

    So instead of just the function above in functions.php we use another function to call it. but this function would be called in the templates

    function category_page() {
        add_filter('page_link', 'permalink_untrailingslashit');
        add_filter('post_type_link', 'permalink_untrailingslashit');
    }
    
    function permalink_untrailingslashit($link) {
    	return untrailingslashit($link);
    }

    so now we have that.

    In your header.php or other template

    We determine if the page is a category or single post/page

    if(is_page()) {
        category_page();
    }

    Or something to that effect, so we only call that function if we're on a page.

    Hope this helps

    Thanks

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.