add_action('init', 'post_type_producten');
add_action( 'init', 'create_producten_taxonomies', 0 );
add_filter('post_type', 'projecten_permalinks', 1, 3);
add_filter('post_type_link', 'projecten_permalinks', 1, 3);
// ## PROJECTEN CUSTOM POST TYPE
function post_type_producten() {
$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' => __('Product bewerken'),
'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' => __('Projecten'),
'labels' => $labels,
'public' => true,
'hierarchical' => false,
'show_ui' => true,
'supports' => array('title','editor','thumbnail','revisions'),
'has_archive' => true,
'menu_position' => 5,
'taxonomies' => array('productCat'),
'rewrite' => array(
'slug' => 'producten/%productCat%',
'with_front' => true,
'hierarchical' => true
)
);
register_post_type('producten', $args);
flush_rewrite_rules();
}
//taxonomies registreren
function create_producten_taxonomies()
{
// productCat
$labels = array(
'name' => _x( 'Product categorieën', 'taxonomy general name' ),
'singular_name' => _x( 'Product categorieën', 'taxonomy singular name' ),
'search_items' => __( 'Zoek Product 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' => __( 'Categorieën' ),
);
register_taxonomy('productCat',array('producten'), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'producten/%productCat%',
'hierarchical' => true,
'with_front' => false )
));
flush_rewrite_rules();
}
// Permalinks projecten
function projecten_permalinks( $post_link, $id = 0, $leavename = FALSE ) {
if ( strpos('%productCat%', $post_link) < 0 ) {
return $post_link;
}
$post = get_post($id);
if ( !is_object($post) || $post->post_type != 'producten' ) {
return $post_link;
}
$terms = wp_get_object_terms($post->ID, 'productCat');
//print_r($terms);
if ( !$terms ) {
return str_replace('%productCat%', '', $post_link);
}
$perm = $terms[0]->slug;
if(!empty($terms[1]->slug)) {
$perm = $terms[0]->slug.'/'.$terms[1]->slug;
}
return str_replace('%productCat%', $perm, $post_link);
}