This is my first attempt at working with WordPress rewrite rules so please bear with me. The issue is that all the items I add to my portfolio have multiple categories. I would like to remove the category from the url when displaying the portfolio posts.
site.com/portfolio
-> Works
site.com/portfolio/category/
-> Works
site.com/portfolio/category/post-added-to-portfolio/
-> Works but I don't want it to
site.com/portfolio/post-added-to-portfolio/
-> Does not Work but it should
/* Post Type: Portfolio */
$labels = array(
"name" => __( "Portfolio", "" ),
"singular_name" => __( "Portfolio", "" ),
);
$args = array(
"label" => __( "Portfolio", "" ),
"labels" => $labels,
"description" => "",
"public" => true,
"publicly_queryable" => true,
"show_ui" => true,
"show_in_rest" => false,
"rest_base" => "",
"has_archive" => "portfolio",
"show_in_menu" => true,
"exclude_from_search" => false,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" => true,
"rewrite" => array( "slug" => "portfolio", "with_front" => true ),
"query_var" => true,
"supports" => array( "title", "editor" ),
"taxonomies" => array( "services" ),
);
register_post_type( "portfolio", $args );
/* Taxonomy: Services */
$labels = array(
"name" => __( "Services", "" ),
"singular_name" => __( "Service", "" ),
);
$args = array(
"label" => __( "Services", "" ),
"labels" => $labels,
"public" => true,
"hierarchical" => true,
"label" => "Services",
"show_ui" => true,
"show_in_menu" => true,
"show_in_nav_menus" => true,
"query_var" => true,
"rewrite" => array( 'slug' => 'portfolio', 'with_front' => true, ),
"show_admin_column" => false,
"show_in_rest" => false,
"rest_base" => "",
"show_in_quick_edit" => false,
);
register_taxonomy( "services", array( "portfolio" ), $args );
// handle redirects for taxonomy
add_action('generate_rewrite_rules', 'generate_taxonomy_rewrite_rules');
function generate_taxonomy_rewrite_rules( $wp_rewrite ) {
$rules = array();
$post_types = get_post_types( array( 'name' => 'portfolio', 'public' => true, '_builtin' => false ), 'objects' );
$taxonomies = get_taxonomies( array( 'name' => 'services', 'public' => true, '_builtin' => false ), 'objects' );
foreach ( $post_types as $post_type ) {
$post_type_name = $post_type->name;
$post_type_slug = $post_type->rewrite['slug'];
foreach ( $taxonomies as $taxonomy ) {
if ( $taxonomy->object_type[0] == $post_type_name ) {
$terms = get_categories( array( 'type' => $post_type_name, 'taxonomy' => $taxonomy->name, 'hide_empty' => 0 ) );
foreach ( $terms as $term ) {
$rules[$post_type_slug . '/' . $term->slug . '/?$'] = 'index.php?' . $term->taxonomy . '=' . $term->slug;
$rules[$post_type_slug . '/' . $term->slug . '/page/?([0-9]{1,})/?$'] = 'index.php?' . $term->taxonomy . '=' . $term->slug . '&paged=' . $wp_rewrite->preg_index( 1 );
}
}
}
}
$wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
http://example.com/%postname%/
, you'd get the "portfolio" URL structure that you wanted to have; i.e. example.com/portfolio/sample-portfolio, where sample-portfolio is the "portfolio" slug. Is that not happening, or are you using a different Permalink structure? – Instepsite.com/portfolio/post-added-to-portfolio/
is working for me. andsite.com/portfolio/category/post-added-to-portfolio/
do not working for me. what would be your permalink structure? – Berneicebernellesite.com/portfolio/post-added-to-portfolio/
notsite.com/portfolio/category/post-added-to-portfolio/
– Every