WordPress Development Asked by KWA on October 1, 2020
I am trying to create custom post with custom taxonomy and categories. Structure is like:
Product:
Custom post type create:
function create_alco_custom_post() {
$review_slug = 'reviews';
$labels = array(
'name' => __( 'Recenzje' ),
'singular_name' => __( 'Recenzja' ),
'add_new' => __( 'Nowa recenzja' ),
'add_new_item' => __( 'Dodaj nową recenzje' ),
'edit_item' => __( 'Edytuj recenzje' ),
'new_item' => __( 'Nowa recenzja' ),
'view_item' => __( 'Zobacz recenzje' ),
'search_items' => __( 'Szukaj w recenzjach' ),
'not_found' => __( 'Nie znaleziono recenzji' ),
'not_found_in_trash' => __( 'Nie znaleziono recenzji w koszu' ),
);
$args = array(
'labels' => $labels,
'menu_icon' => 'dashicons-format-quote',
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => '%review-category%','with_front' => true),
'hierarchical' => true,
'menu_position' => 0,
'exclude_from_search' => false,
'supports' => array(
'title',
'editor',
'thumbnail'
),
'taxonomies' => array('category')
);
register_post_type( 'review', $args );
}
add_action( 'init', 'create_alco_custom_post' );
Then I add taxonomy:
$labels = array(
'name' => __( 'Producenci', 'alkowiki' ),
'singular_name' => __( 'Producent', 'alkowiki' ),
'search_items' => __( 'Wyszukaj wśród producentów', 'alkowiki' ),
'popular_items' => __( 'Popularni producenci', 'alkowiki' ),
'all_items' => __( 'Wszystcy producenci', 'alkowiki' ),
'parent_item' => __( 'Producent rodzic', 'alkowiki' ),
'edit_item' => __( 'Edytuj producenta', 'alkowiki' ),
'update_item' => __( 'Aktualizuj producenta', 'alkowiki' ),
'add_new_item' => __( 'Dodaj nowego producenta', 'alkowiki' ),
'new_item_name' => __( 'Nowy producent', 'alkowiki' ),
'separate_items_with_commas' => __( 'Oddziel producentów przecinkami', 'alkowiki' ),
'add_or_remove_items' => __( 'Dodaj lub usuń producentów', 'alkowiki' ),
'choose_from_most_used' => __( 'Wybierz z najczęściej wykorzystywanych producentów', 'alkowiki' )
);
$args = array(
'labels' => $labels,
'public' => true,
'hierarchical' => true,
'show_ui' => true,
'show_in_nav_menus' => true,
'query_var' => true
);
register_taxonomy( 'producents', 'review', $args );
And finally I change link structure:
add_filter('post_link', 'category_permalink', 1, 3);
add_filter('post_type_link', 'category_permalink', 1, 3);
function category_permalink($permalink, $post_id, $leavename) {
if (strpos($permalink, '%review-category%') === FALSE) return $permalink;
// Get post
$post = get_post($post_id);
if (!$post) return $permalink;
// Get taxonomy terms
$terms = wp_get_object_terms($post->ID, 'category');
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0]))
$taxonomy_slug = $terms[0]->slug;
else $taxonomy_slug = 'nieskateryzowane';
return str_replace('%review-category%', $taxonomy_slug, $permalink);
}
My post structure in WP settings is /%category%/%postname%/
And post URL’s are created properly, category archives are displaying posts properly, permlinks are created as I wanted, but when I go to product site (URL structure site/category/product-name) I receive 404. When I visit URL with taxonomy instead of category (site/taxonomy/product-name) I’m being redirected to /category/product-name site, and it also ends with 404.
I’m struggling with this for days, any ideas?
EDIT:
Unfortunately I had to switch back permalinks to default (%postname%) – I want default WP Posts to have URL structure like this. I’ve also change default category (which is connected with default posts) to custom taxonomy called “my-category”, and register it for my custom post type. So now there are 2 taxonomies, (same as first one inserted). Archive pages for them gives 404, and product page also still gives 404. How should I set rewrite structure?
I’ll precise what I want to achieve:
You can follow three steps for solution
1) Go to settings > permalinks
and change the permalink as plane or default then save it.
2) Now Go to your site home page and refresh your home page. Now check the category pages.
3) Now go again settings > permalinks
and now change it /%category%/%postname%/
And now go to home page refresh it and check your category pages
Hop it'll help you.
Answered by deemi-D-nadeem on October 1, 2020
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP