I have a custom post type called event. The URL structure looks like this.

http://my-url.com/events/2016/08/21/workshop

Where “workshop” is the name of the event. Right now the template will show different data depending on the query string. For example this URL:

http://my-url.com/events/2016/08/21/workshop?sub=files

Will loop out some files. I’m adding the query string so i can do that in the same template.

<?php
$sub = isset( $_GET['sub'] ) ? $_GET['sub'] : null;
switch ( $sub ) {
    case "files":
        echo "TODO: Loop out files";
        break;
    case "participants":
        echo "TODO: Loop out participants";
        break;
    case "agenda":
        echo "TODO: Write out the agenda!";
        break;
        <!-- etc -->
}
?>

But i don’t like this URL structure! I’d like the URL to be:

http://my-url.com/events/2016/08/21/workshop/files

So that i in the “Event” template (/workshop/) can look at the URL structure instead of the query string. The problem is that adding /filesto the URL will make wordpress look for a page that doesn’t exist and give a 404.

Is it possible to do some URL wizardry so the template (and post data) of this url:

http://my-url.com/events/2016/08/21/workshop/

Also kicks in with this URL:

http://my-url.com/events/2016/08/21/workshop/agenda/

2 Answers
2

Put this code in your functions.php

function the_dramatist_url_rewrite() {
    global $wp_rewrite;
    add_rewrite_tag('%sub%', '([^&]+)');
    add_rewrite_rule(
        '^events/%year%/%monthnum%/%day%/%postname%/?', 
        'index.php/events/%year%/%monthnum%/%day%/%postname%?sub=$matches[1]', 
        'top'
    );
}
add_action('init', 'the_dramatist_url_rewrite', 10, 0);

The go to the permalinks page in the settings menu at dashboard and hit Save Changes button. Hope this is gonna help.

Leave a Reply

Your email address will not be published. Required fields are marked *