Reputation: 1027
I have to have this URL structure: http://url.pl/custompostype/taxonomy/singlepost
I have such, but I also have 404 page instead of post or list of posts
. I know that there is a very popular problem, but all the tips from the Google do not work for me :( I think, that I have to change 'with_front'
into false and it should work? I changed a lot of parameters in multiple ways and it still does not work.
Here is my code:
function learning() {
$labels = array(
'name' => _x( 'Myposttype', 'Post Type General Name', 'text_domain' ),
'singular_name' => _x( 'Myposttype', 'Post Type Singular Name', 'text_domain' ),
'menu_name' => __( 'Myposttype', 'text_domain' ),
'name_admin_bar' => __( 'Myposttype', 'text_domain' ),
'archives' => __( 'Archiwa', 'text_domain' ),
'attributes' => __( 'Atrybuty', 'text_domain' ),
'parent_item_colon' => __( 'Parent Item:', 'text_domain' ),
'all_items' => __( 'Wszystkie', 'text_domain' ),
'add_new_item' => __( 'Dodaj nowy post', 'text_domain' ),
'add_new' => __( 'Dodaj nowy', 'text_domain' ),
'new_item' => __( 'Nowy', 'text_domain' ),
'edit_item' => __( 'Edytuj', 'text_domain' ),
'update_item' => __( 'Zaktualizuj', 'text_domain' ),
'view_item' => __( 'Zobacz', 'text_domain' ),
'view_items' => __( 'Zobacz', 'text_domain' ),
'search_items' => __( 'Szukaj', 'text_domain' ),
'featured_image' => __( 'Obrazek wyróżniający', 'text_domain' ),
'set_featured_image' => __( 'Ustaw obrazek wyróżniający', 'text_domain' ),
'remove_featured_image' => __( 'Usuń obrazek', 'text_domain' ),
);
$args = array(
'label' => __( 'Myposttype', 'text_domain' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'thumbnail', 'page-attributes' ),
'taxonomies' => array( 'customtaxonomy'),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-format-chat',
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
'rewrite' => array('with_front' => false ),
);
register_post_type( 'myposttype', $args );
}
add_action( 'init', 'learning', 0 );
// Register Custom Taxonomy
function myposttype2() {
$labels = array(
'name' => _x( 'CustomTaxonomy', 'Taxonomy General Name', 'text_domain' ),
'singular_name' => _x( 'CustomTaxonomy', 'Taxonomy Singular Name', 'text_domain' ),
'menu_name' => __( 'CustomTaxonomy', 'text_domain' ),
'all_items' => __( 'Wszystkie', 'text_domain' ),
'new_item_name' => __( 'Dodaj nowy', 'text_domain' ),
'add_new_item' => __( 'Dodaj nowy', 'text_domain' ),
'edit_item' => __( 'Edytuj', 'text_domain' ),
'update_item' => __( 'Zaktualizuj', 'text_domain' ),
'view_item' => __( 'Zobacz', 'text_domain' ),
'separate_items_with_commas' => __( 'Oddziel kolejne tagi przecinkami', 'text_domain' ),
'choose_from_most_used' => __( 'Wybierz spośród popularnych', 'text_domain' ),
);
$rewrite = array(
'slug' => 'myposttype/customtaxonomy',
'with_front' => false,
'hierarchical' => true,
//'has_archive' => 'myposttype',
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'rewrite' => $rewrite,
);
register_taxonomy( 'customtaxonomy', array( 'myposttype' ), $args );
}
add_action( 'init', 'myposttype2', 0 );
Upvotes: 0
Views: 866
Reputation: 2011
Rest the permalinks and also make sure same custom post type slug is not used any where in the website, this may also create the issue.
Add custom taxonomy in url with following way.
Change custom post type rewrite array item with following
'rewrite' => array('slug' => '%customtaxonomy%', 'with_front' => false ),
and then add following filter in theme's functions.php file.
function d_reset_permlinks( $post_link, $post ){
if ( is_object( $post ) && $post->post_type == 'myposttype' ){
$terms = wp_get_object_terms( $post->ID, 'customtaxonomy' );
if( $terms ){
return str_replace( '%customtaxonomy%' , $terms[0]->slug , $post_link );
}
}
return $post_link;
}
add_filter( 'post_type_link', 'd_reset_permlinks', 1, 2 );
Now reset the permlinks and it will work . you can see its tasted in my local system http://prntscr.com/jnxvyo
Upvotes: 0
Reputation: 9373
Please do one thing go to admin dashboard, Setting->Permalinks
change permalinks to plain
and save
it.
And again change permalinks to Post Name
and save it.
It will resolve 404 issue.
Upvotes: 1