[wp] Custom post permalink geeft 404
-
Zit al een tijdje te zeulen met het volgende probleem. Ik ben niet een die-hard coder, misschien dat het daar aan ligt, maar toch snap ik het probleem niet. Het probleem is als volgt:
In WordPress heb ik een custom post opgezet met een daarbij behorende custom taxonomy. Nu lukt het mij niet om de custom taxonomy werkend te krijgen in de permalink. De permlink zou er als volgt uit moeten zien:
http://www.domein.nl/producten/categorie-1/productnaam/Eerst maar even de code:
/* =================================== CUSTOM POST TYPE -> PRODUCTEN =================================== */ add_action('init', 'post_type_products'); add_action('admin_init', 'admin_init_products'); add_action("save_post", "save_custom_fields_products"); add_action('init', 'create_taxonomies_products'); add_filter('post_updated_messages', 'updated_messages_products'); function post_type_products() { $labels = array( 'name' => _x('Producten', 'post type general name'), 'singular_name' => _x('Producten', 'post type singular name'), 'add_new' => _x('Nieuw product', 'producten'), 'add_new_item' => __('Nieuw product toevoegen'), 'edit_item' => __('Wijzig product'), 'new_item' => __('Nieuw product'), 'view_item' => __('Bekijk product'), 'search_items' => __('Zoek producten'), 'not_found' => __('Geen producten gevonden'), 'not_found_in_trash' => __('Geen producten in de prullenbak'), 'parent_item_colon' => '', ); $args = array( 'label' => __('Producten'), 'labels' => $labels, 'public' => true, 'show_ui' => true, 'menu_postition' => 5, 'hierarchical' => false, 'has_archive' => true, 'taxonomies' => array('productCat'), 'supports' => array('title','editor','thumbnail','revisions'), 'rewrite' => array( 'slug' => 'producten/%products_cat%', 'with_front' => false ) ); register_post_type('products', $args); flush_rewrite_rules(); } function create_taxonomies_products() { $labels = array( 'name' => _x( 'Product categorieën', 'taxonomy general name' ), 'singular_name' => _x( 'Product categorieën', 'taxonomy singular name' ), 'search_items' => __( 'Zoek categorieën' ), 'all_items' => __( 'Alle categorieën' ), 'parent_item' => __( 'Bovenliggende categorie' ), 'parent_item_colon' => __( 'Hoofd categorie:' ), 'edit_item' => __( 'Wijzig categorie' ), 'update_item' => __( 'Update categorie' ), 'add_new_item' => __( 'Nieuwe categorie' ), 'new_item_name' => __( 'Nieuwe categorie naam' ), 'menu_name' => __( 'Product categorieën' ), ); register_taxonomy('productCat',array('products'), array( 'hierarchical' => true, 'labels' => $labels, 'show_ui' => true, 'show_in_nav_menus' => true, 'query_var' => true, 'has_archive' => true, 'query_var' => true, 'rewrite' => array( 'slug' => 'producten', 'with_front' => false ), )); } // Permalinks voor producten add_filter('post_link', 'products_permalink', 10, 3); add_filter('post_type_link', 'products_permalink', 10, 3); function products_permalink($permalink, $post_id, $leavename) { if (strpos($permalink, '%products_cat%') === FALSE) return $permalink; // Get post $post = get_post($post_id); if (!$post) return $permalink; // Get taxonomy terms $terms = wp_get_object_terms($post->ID, 'productCat'); if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) { if($terms[0]->parent != 0 && count($terms) == 2) { $taxonomy_slug = $terms[1]->slug.'/'.$terms[0]->slug; } elseif(count($terms) == 2) { $taxonomy_slug = $terms[0]->slug.'/'.$terms[1]->slug; } else { $taxonomy_slug = $terms[0]->slug; } } else { $taxonomy_slug = 'algemeen'; } return str_replace('%products_cat%', $taxonomy_slug, $permalink); }
Het stukje %products_cat% wordt correct vervangen. Daarnaast heb ik nergens dubbele namen (voor zover ik weet). Alleen telkens als ik naar de product-item pagina ga, dan krijg ik een 404. De categorie pagina werkt wel gewoon (dus http://www.domein.nl/producten/categorie-1/ komt uit op een overzicht van die categorie). Ik update de permalinks constant, thema’s geswitched etc.
Als ik de de categorie ertussen uithaal werkt het wel allemaal. Wie ontdekt de fout die ik heb gemaakt. Ik heb het al eerder werkend gehad op een andere site (wel iets anders), maar nu lukt het mij niet. |:(
- Het onderwerp ‘[wp] Custom post permalink geeft 404’ is gesloten voor nieuwe reacties.