Skip to main content
replaced http://wordpress.stackexchange.com/ with https://wordpress.stackexchange.com/
Source Link

I have been working on this and have arrived at a solution I am happy with. I hope it makes sense at a WordPress level!

I have decided to keep the taxonomy term in the urls, I want to mess with the default WordPress behaviour as little as possible, and the URL still reads fine. (eg: www.mysite.com/club/juggling-club)

I have used a rewrite rule to resolve my post_type query var (eg: www.mysite.com/club/juggling-club/?post_type=events becomes www.mysite.com/club/juggling-club/events to list only events at the juggling club)

function mytest_rewrite_rule() {
    add_rewrite_rule(
        'club/([\-\w+]*)/([\w+]*)/?$', 
        'index.php?club=$matches[1]&post_type=$matches[2]',
        'top'
    );
}
add_action('init', 'mytest_rewrite_rule', 10, 0);

To follow on from that structure, I wanted my custom posts to have a url www.mysite.com/club/juggling-club/event/sponsored-juggle. With help from this postthis post I created a custom term in my cpt's rewrite slug (club/%club%/event) that upholds my url pattern. WordPress needs some code to handle this:

function mytest_cpt_rewrite( $link, $post ) {
if ( $post->post_type === 'events' ) {
    if ( $terms = get_the_terms( $post->ID, 'club' ) )
            $link = str_replace( '%club%', current( $terms )->slug, $link );
        }
    return $link;
}
add_filter( 'post_type_link', 'mytest_cpt_rewrite', 10, 2 );

In summary, I have ended up with a logical structure:

www.mysite.com/club/juggling-club - Will be a general club landing page
www.mysite.com/club/juggling-club/events - Show all of a custom post type
www.mysite.com/club/juggling-club/event/sponsored-juggle - Show a specific post

I am considering making selection of club a required action when posting, as content will always need to belong to a club, and this should protect the URL pattern I have created.

I will see how things go with templates, I also plan on injecting the taxonomy term as a body css class to help with targetted styling of some elements.

Any further comments ideas still welcome! I hope this helps someone organising content.

I have been working on this and have arrived at a solution I am happy with. I hope it makes sense at a WordPress level!

I have decided to keep the taxonomy term in the urls, I want to mess with the default WordPress behaviour as little as possible, and the URL still reads fine. (eg: www.mysite.com/club/juggling-club)

I have used a rewrite rule to resolve my post_type query var (eg: www.mysite.com/club/juggling-club/?post_type=events becomes www.mysite.com/club/juggling-club/events to list only events at the juggling club)

function mytest_rewrite_rule() {
    add_rewrite_rule(
        'club/([\-\w+]*)/([\w+]*)/?$', 
        'index.php?club=$matches[1]&post_type=$matches[2]',
        'top'
    );
}
add_action('init', 'mytest_rewrite_rule', 10, 0);

To follow on from that structure, I wanted my custom posts to have a url www.mysite.com/club/juggling-club/event/sponsored-juggle. With help from this post I created a custom term in my cpt's rewrite slug (club/%club%/event) that upholds my url pattern. WordPress needs some code to handle this:

function mytest_cpt_rewrite( $link, $post ) {
if ( $post->post_type === 'events' ) {
    if ( $terms = get_the_terms( $post->ID, 'club' ) )
            $link = str_replace( '%club%', current( $terms )->slug, $link );
        }
    return $link;
}
add_filter( 'post_type_link', 'mytest_cpt_rewrite', 10, 2 );

In summary, I have ended up with a logical structure:

www.mysite.com/club/juggling-club - Will be a general club landing page
www.mysite.com/club/juggling-club/events - Show all of a custom post type
www.mysite.com/club/juggling-club/event/sponsored-juggle - Show a specific post

I am considering making selection of club a required action when posting, as content will always need to belong to a club, and this should protect the URL pattern I have created.

I will see how things go with templates, I also plan on injecting the taxonomy term as a body css class to help with targetted styling of some elements.

Any further comments ideas still welcome! I hope this helps someone organising content.

I have been working on this and have arrived at a solution I am happy with. I hope it makes sense at a WordPress level!

I have decided to keep the taxonomy term in the urls, I want to mess with the default WordPress behaviour as little as possible, and the URL still reads fine. (eg: www.mysite.com/club/juggling-club)

I have used a rewrite rule to resolve my post_type query var (eg: www.mysite.com/club/juggling-club/?post_type=events becomes www.mysite.com/club/juggling-club/events to list only events at the juggling club)

function mytest_rewrite_rule() {
    add_rewrite_rule(
        'club/([\-\w+]*)/([\w+]*)/?$', 
        'index.php?club=$matches[1]&post_type=$matches[2]',
        'top'
    );
}
add_action('init', 'mytest_rewrite_rule', 10, 0);

To follow on from that structure, I wanted my custom posts to have a url www.mysite.com/club/juggling-club/event/sponsored-juggle. With help from this post I created a custom term in my cpt's rewrite slug (club/%club%/event) that upholds my url pattern. WordPress needs some code to handle this:

function mytest_cpt_rewrite( $link, $post ) {
if ( $post->post_type === 'events' ) {
    if ( $terms = get_the_terms( $post->ID, 'club' ) )
            $link = str_replace( '%club%', current( $terms )->slug, $link );
        }
    return $link;
}
add_filter( 'post_type_link', 'mytest_cpt_rewrite', 10, 2 );

In summary, I have ended up with a logical structure:

www.mysite.com/club/juggling-club - Will be a general club landing page
www.mysite.com/club/juggling-club/events - Show all of a custom post type
www.mysite.com/club/juggling-club/event/sponsored-juggle - Show a specific post

I am considering making selection of club a required action when posting, as content will always need to belong to a club, and this should protect the URL pattern I have created.

I will see how things go with templates, I also plan on injecting the taxonomy term as a body css class to help with targetted styling of some elements.

Any further comments ideas still welcome! I hope this helps someone organising content.

edited body
Source Link

I have been working on this and have arrived at a solotionsolution I am happy with. I hope it makes sense at a WordPress level!

I have decided to keep the taxonomy term in the urls, I want to mess with the default WordPress behaviour as little as possible, and the URL still reads fine. (eg: www.mysite.com/club/juggling-club)

I have used a rewrite rule to resolve my post_type query var (eg: www.mysite.com/club/juggling-club/?post_type=events becomes www.mysite.com/club/juggling-club/events to list only events at the juggling club)

function mytest_rewrite_rule() {
    add_rewrite_rule(
        'club/([\-\w+]*)/([\w+]*)/?$', 
        'index.php?club=$matches[1]&post_type=$matches[2]',
        'top'
    );
}
add_action('init', 'mytest_rewrite_rule', 10, 0);

To follow on from that structure, I wanted my custom posts to have a url www.mysite.com/club/juggling-club/event/sponsored-juggle. With help from this post I created a custom term in my cpt's rewrite slug (club/%club%/event) that upholds my url pattern. WordPress needs some code to handle this:

function mytest_cpt_rewrite( $link, $post ) {
if ( $post->post_type === 'events' ) {
    if ( $terms = get_the_terms( $post->ID, 'club' ) )
            $link = str_replace( '%club%', current( $terms )->slug, $link );
        }
    return $link;
}
add_filter( 'post_type_link', 'mytest_cpt_rewrite', 10, 2 );

In summary, I have ended up with a logical structure:

www.mysite.com/club/juggling-club - Will be a general club landing page
www.mysite.com/club/juggling-club/events - Show all of a custom post type
www.mysite.com/club/juggling-club/event/sponsored-juggle - Show a specific post

I am considering making selection of club a required action when posting, as content will always need to belong to a club, and this should protect the URL pattern I have created.

I will see how things go with templates, I also plan on injecting the taxonomy term as a body css class to help with targetted styling of some elements.

Any further comments ideas still welcome! I hope this helps someone organising content.

I have been working on this and have arrived at a solotion I am happy with. I hope it makes sense at a WordPress level!

I have decided to keep the taxonomy term in the urls, I want to mess with the default WordPress behaviour as little as possible, and the URL still reads fine. (eg: www.mysite.com/club/juggling-club)

I have used a rewrite rule to resolve my post_type query var (eg: www.mysite.com/club/juggling-club/?post_type=events becomes www.mysite.com/club/juggling-club/events to list only events at the juggling club)

function mytest_rewrite_rule() {
    add_rewrite_rule(
        'club/([\-\w+]*)/([\w+]*)/?$', 
        'index.php?club=$matches[1]&post_type=$matches[2]',
        'top'
    );
}
add_action('init', 'mytest_rewrite_rule', 10, 0);

To follow on from that structure, I wanted my custom posts to have a url www.mysite.com/club/juggling-club/event/sponsored-juggle. With help from this post I created a custom term in my cpt's rewrite slug (club/%club%/event) that upholds my url pattern. WordPress needs some code to handle this:

function mytest_cpt_rewrite( $link, $post ) {
if ( $post->post_type === 'events' ) {
    if ( $terms = get_the_terms( $post->ID, 'club' ) )
            $link = str_replace( '%club%', current( $terms )->slug, $link );
        }
    return $link;
}
add_filter( 'post_type_link', 'mytest_cpt_rewrite', 10, 2 );

In summary, I have ended up with a logical structure:

www.mysite.com/club/juggling-club - Will be a general club landing page
www.mysite.com/club/juggling-club/events - Show all of a custom post type
www.mysite.com/club/juggling-club/event/sponsored-juggle - Show a specific post

I am considering making selection of club a required action when posting, as content will always need to belong to a club, and this should protect the URL pattern I have created.

I will see how things go with templates, I also plan on injecting the taxonomy term as a body css class to help with targetted styling of some elements.

Any further comments ideas still welcome! I hope this helps someone organising content.

I have been working on this and have arrived at a solution I am happy with. I hope it makes sense at a WordPress level!

I have decided to keep the taxonomy term in the urls, I want to mess with the default WordPress behaviour as little as possible, and the URL still reads fine. (eg: www.mysite.com/club/juggling-club)

I have used a rewrite rule to resolve my post_type query var (eg: www.mysite.com/club/juggling-club/?post_type=events becomes www.mysite.com/club/juggling-club/events to list only events at the juggling club)

function mytest_rewrite_rule() {
    add_rewrite_rule(
        'club/([\-\w+]*)/([\w+]*)/?$', 
        'index.php?club=$matches[1]&post_type=$matches[2]',
        'top'
    );
}
add_action('init', 'mytest_rewrite_rule', 10, 0);

To follow on from that structure, I wanted my custom posts to have a url www.mysite.com/club/juggling-club/event/sponsored-juggle. With help from this post I created a custom term in my cpt's rewrite slug (club/%club%/event) that upholds my url pattern. WordPress needs some code to handle this:

function mytest_cpt_rewrite( $link, $post ) {
if ( $post->post_type === 'events' ) {
    if ( $terms = get_the_terms( $post->ID, 'club' ) )
            $link = str_replace( '%club%', current( $terms )->slug, $link );
        }
    return $link;
}
add_filter( 'post_type_link', 'mytest_cpt_rewrite', 10, 2 );

In summary, I have ended up with a logical structure:

www.mysite.com/club/juggling-club - Will be a general club landing page
www.mysite.com/club/juggling-club/events - Show all of a custom post type
www.mysite.com/club/juggling-club/event/sponsored-juggle - Show a specific post

I am considering making selection of club a required action when posting, as content will always need to belong to a club, and this should protect the URL pattern I have created.

I will see how things go with templates, I also plan on injecting the taxonomy term as a body css class to help with targetted styling of some elements.

Any further comments ideas still welcome! I hope this helps someone organising content.

Source Link

I have been working on this and have arrived at a solotion I am happy with. I hope it makes sense at a WordPress level!

I have decided to keep the taxonomy term in the urls, I want to mess with the default WordPress behaviour as little as possible, and the URL still reads fine. (eg: www.mysite.com/club/juggling-club)

I have used a rewrite rule to resolve my post_type query var (eg: www.mysite.com/club/juggling-club/?post_type=events becomes www.mysite.com/club/juggling-club/events to list only events at the juggling club)

function mytest_rewrite_rule() {
    add_rewrite_rule(
        'club/([\-\w+]*)/([\w+]*)/?$', 
        'index.php?club=$matches[1]&post_type=$matches[2]',
        'top'
    );
}
add_action('init', 'mytest_rewrite_rule', 10, 0);

To follow on from that structure, I wanted my custom posts to have a url www.mysite.com/club/juggling-club/event/sponsored-juggle. With help from this post I created a custom term in my cpt's rewrite slug (club/%club%/event) that upholds my url pattern. WordPress needs some code to handle this:

function mytest_cpt_rewrite( $link, $post ) {
if ( $post->post_type === 'events' ) {
    if ( $terms = get_the_terms( $post->ID, 'club' ) )
            $link = str_replace( '%club%', current( $terms )->slug, $link );
        }
    return $link;
}
add_filter( 'post_type_link', 'mytest_cpt_rewrite', 10, 2 );

In summary, I have ended up with a logical structure:

www.mysite.com/club/juggling-club - Will be a general club landing page
www.mysite.com/club/juggling-club/events - Show all of a custom post type
www.mysite.com/club/juggling-club/event/sponsored-juggle - Show a specific post

I am considering making selection of club a required action when posting, as content will always need to belong to a club, and this should protect the URL pattern I have created.

I will see how things go with templates, I also plan on injecting the taxonomy term as a body css class to help with targetted styling of some elements.

Any further comments ideas still welcome! I hope this helps someone organising content.