Skip to content

Instantly share code, notes, and snippets.

@braddalton
Created May 24, 2019 20:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save braddalton/8ef23b07bbaef001cf3f178a3a34782c to your computer and use it in GitHub Desktop.
Save braddalton/8ef23b07bbaef001cf3f178a3a34782c to your computer and use it in GitHub Desktop.
Custom Permalink Structure for Taxonomies Added to CPT's https://wp.me/p1lTu0-ghd
//* Create Golf Courses custom post type
add_action( 'init', 'golf_courses_post_type' );
function golf_courses_post_type() {
register_post_type( 'golf-courses',
array(
'labels' => array(
'name' => __( 'Golf Courses', 'genesis' ),
'singular_name' => __( 'Golf Course', 'genesis' ),
),
'has_archive' => true,
'hierarchical' => true,
'menu_icon' => 'dashicons-flag',
'public' => true,
'rewrite' => array( 'slug' => 'golf-course/%country-type%/%region-type%', 'with_front' => false ),
'query_var' => true,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'revisions', 'page-attributes', 'genesis-seo', 'genesis-cpt-archives-settings' ),
'taxonomies' => array( 'country-type', 'region-type' ),
)
);
}
//* Create golf course custom taxonomy
add_action( 'init', 'golf_courses_taxonomy' );
function golf_courses_taxonomy() {
register_taxonomy( 'country-type', 'golf-courses',
array(
'labels' => array(
'name' => _x( 'Country', 'taxonomy general name', 'genesis' ),
'add_new_item' => __( 'Add New Country', 'genesis' ),
'new_item_name' => __( 'New Country', 'genesis' ),
),
'exclude_from_search' => true,
'has_archive' => true,
'hierarchical' => true,
'rewrite' => array( 'slug' => 'country-type', 'with_front' => false ),
'show_ui' => true,
'show_tagcloud' => false,
)
);
register_taxonomy( 'region-type', 'golf-courses',
array(
'labels' => array(
'name' => _x( 'Region', 'taxonomy general name', 'genesis' ),
'add_new_item' => __( 'Add New Region', 'genesis' ),
'new_item_name' => __( 'New Region', 'genesis' ),
),
'exclude_from_search' => true,
'has_archive' => true,
'hierarchical' => true,
'rewrite' => array( 'slug' => 'region-type', 'with_front' => false ),
'show_ui' => true,
'show_tagcloud' => false,
)
);
}
add_filter( 'post_type_link', 'golf_courses_country_post_type_link', 10, 2 );
function golf_courses_country_post_type_link( $link, $post ) {
if ( $post->post_type == 'golf-courses' ) {
if ( $cats = get_the_terms( $post->ID, 'country-type' ) ) {
$link = str_replace( '%country-type%', current($cats)->slug, $link );
}
}
return $link;
}
add_filter( 'post_type_link', 'golf_courses_region_post_type_link', 10, 2 );
function golf_courses_region_post_type_link( $link, $post ) {
if ( $post->post_type == 'golf-courses' ) {
if ( $cats = get_the_terms( $post->ID, 'region-type' ) ) {
$link = str_replace( '%region-type%', current($cats)->slug, $link );
}
}
return $link;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment