function create_posttype() {
register_post_type( 'events',
array(
'labels' => array(
'name' => __( 'Events' ),
'singular_name' => __( 'Event' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => '%city%/events'),
)
);
}
add_action( 'init', 'create_posttype' );
register_taxonomy('cities', array('events'), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'city' ),
));
function wpa_permalinks( $post_link, $post ){
if ( is_object( $post ) && $post->post_type == 'events' ){
$terms = wp_get_object_terms( $post->ID, 'cities' );
if( $terms ){
return str_replace( '%city%' , $terms[0]->slug , $post_link );
}
}
return $post_link;
}
add_filter( 'post_type_link', 'wpa_permalinks', 1, 2 );
The permalink are correctly structured (ex. http://www.example.org/new-york/events/event-name
) but I have two issues:
- The template single-events.php is not taken and it shows 404.
- Same for the archive template.
http://www.example.org/new-york/events
should display the archive.
How to solve?
%city%
with%cities%
%city%
was just a placeholder with a random name. Even the archive page seems working now! Why?