Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* ==========================================================================
- Post Types
- ========================================================================== */
- add_action( 'init', 'my_custom_post_auto' );
- function my_custom_post_auto() {
- $labels = array(
- 'name' => _x( 'Auto', 'post type general name' ),
- 'singular_name' => _x( 'auto', 'post type singular name' ),
- 'add_new' => _x( 'Voeg toe', 'auto' ),
- 'add_new_item' => __( 'Voeg nieuwe auto toe' ),
- 'edit_item' => __( 'Edit auto' ),
- 'new_item' => __( 'Nieuwe auto' ),
- 'all_items' => __( 'Alle autos' ),
- 'view_item' => __( 'Bekijk auto' ),
- 'search_items' => __( 'Zoek auto' ),
- 'not_found' => __( 'Geen autos gevonden' ),
- 'not_found_in_trash' => __( 'Geen autos gevonden' ),
- 'parent_item_colon' => '',
- 'menu_name' => 'Autos'
- );
- $args = array(
- 'labels' => $labels,
- 'description' => '',
- 'public' => true,
- 'menu_position' => 5,
- 'supports' => array( 'title', 'editor', 'thumbnail'),
- 'has_archive' => 'autos',
- 'rewrite' => array('slug' => 'merk/%auto-merk%/%auto-bouwjaar%' ),
- );
- register_post_type( 'auto', $args );
- }
- /* ==========================================================================
- Taxonomies
- ========================================================================== */
- /* Auto Merk taxonomy */
- function auto_merk_taxonomy() {
- $labels = array(
- 'name' => __('Automerken'),
- 'singular_name' => __('Automerk'),
- 'search_items' => __('Zoek automerken'),
- 'all_items' => __('Alle automerken'),
- 'parent_item' => __('Hoofdautomerk'),
- 'parent_item_colon' => __('Hoofdautomerk:'),
- 'edit_item' => __('Bewerk automerk'),
- 'update_item' => __('Werk automerk bij'),
- 'add_new_item' => __('Voeg nieuw automerk toe'),
- 'new_item_name' => __('Nieuw automerk'),
- 'menu_name' => __('Automerken')
- );
- $args = array(
- 'labels' => $labels,
- 'hierarchical' => true,
- 'query_var' => true,
- 'rewrite' => array('slug' => 'merk', 'hierarchical' => true)
- );
- register_taxonomy('auto-merk', 'auto', $args);
- }
- add_action( 'init', 'auto_merk_taxonomy' );
- /* Auto Bouwjaar taxonomy */
- function auto_bouwjaar_taxonomy() {
- $labels = array(
- 'name' => __('Bouwjaren'),
- 'singular_name' => __('Bouwjaar'),
- 'search_items' => __('Zoek bouwjaren'),
- 'all_items' => __('Alle bouwjaren'),
- 'parent_item' => __('Hoofdbouwjaar'),
- 'parent_item_colon' => __('Hoofdbouwjaar:'),
- 'edit_item' => __('Bewerk bouwjaar'),
- 'update_item' => __('Werk bouwjaar bij'),
- 'add_new_item' => __('Voeg nieuw bouwjaar toe'),
- 'new_item_name' => __('Nieuw bouwjaar'),
- 'menu_name' => __('Bouwjaren')
- );
- $args = array(
- 'labels' => $labels,
- 'hierarchical' => true,
- 'query_var' => true,
- 'rewrite' => array('slug' => 'merk', 'hierarchical' => true)
- );
- register_taxonomy('auto-bouwjaar', 'auto', $args);
- }
- add_action( 'init', 'auto_bouwjaar_taxonomy' );
- /* ==========================================================================
- Permalink Structure
- ========================================================================== */
- function filter_post_type_link($link, $post) {
- if ($post->post_type != 'auto')
- return $link;
- if ($cats = get_the_terms($post->ID, 'auto-merk')) { // return both parent and child terms alphabetically
- $link = str_replace('%auto-merk%', get_taxonomy_parents(array_pop($cats)->term_id, 'auto-merk', false, '/', true), $link);
- //$family_id = print_taxonomy_ranks( get_the_terms($post->ID, 'auto-merk') ); // get only child term id
- //$link = str_replace('%auto-merk%', get_taxonomy_parents($family_id, 'auto-merk', false, '/', true), $link);
- }
- return $link;
- }
- add_filter('post_type_link', 'filter_post_type_link', 10, 2);
- /*
- * rank hierarchical taxonomy terms:
- * http://wordpress.stackexchange.com/questions/37285/custom-taxonomy-get-the-terms-listing-in-order-of-parent-child
- */
- function print_taxonomy_ranks($terms = '' ){
- // check input
- if ( empty( $terms ) || is_wp_error( $terms ) || ! is_array( $terms ) )
- return;
- // set id variables to 0 for easy check
- $order_id = $family_id = $subfamily_id = 0;
- // get order
- foreach ( $terms as $term ) {
- if ( $order_id || $term->parent )
- continue;
- $order_id = $term->term_id;
- $order = $term->name;
- }
- // get family
- foreach ( $terms as $term ) {
- if ( $family_id || $order_id != $term->parent )
- continue;
- $family_id = $term->term_id;
- $family = $term->name;
- }
- // get subfamily
- foreach ( $terms as $term ) {
- if ( $subfamily_id || $family_id != $term->parent )
- continue;
- $subfamily_id = $term->term_id;
- $subfamily = $term->name;
- }
- // output
- return $family_id;
- }
- /*
- * Get taxonomy parents:
- * http://wordpress.stackexchange.com/questions/101316/custom-post-type-with-two-hierarchical-custom-taxonomies-strategy-to-generate-b
- */
- function get_taxonomy_parents($id, $taxonomy, $link = false, $nicename = false, $visited = array()) {
- // removed $seperator="/" after $link, otherwise there was a double slash before the post name in the link
- $chain = '';
- $parent = &get_term($id, $taxonomy);
- if (is_wp_error($parent)) {
- return $parent;
- }
- if ($nicename)
- $name = $parent -> slug;
- else
- $name = $parent -> name;
- if ($parent -> parent && ($parent -> parent != $parent -> term_id) && !in_array($parent -> parent, array($visited)) ) {
- $visited[] = $parent -> parent;
- $chain .= get_taxonomy_parents($parent -> parent, $taxonomy, $link, $nicename, $visited); //removed $seperator after $link
- $chain .= "/"; //add a / after every category
- }
- if ($link) {
- // nothing, can't get this working :(
- } else
- $chain .= $name; //don't need the . $separator anymore
- return $chain;
- }
- function filter_post_type_link_due($link, $post) {
- if ($post->post_type != 'auto')
- return $link;
- if ($cats = get_the_terms($post->ID, 'auto-bouwjaar')) {
- $link = str_replace('%auto-bouwjaar%', get_taxonomy_parents(array_pop($cats)->term_id, 'auto-bouwjaar', false, '/', true), $link);
- }
- return $link;
- }
- add_filter('post_type_link', 'filter_post_type_link_due', 10, 2);
- /* ==========================================================================
- Rewrite Rules
- ========================================================================== */
- add_filter('rewrite_rules_array', 'mmp_rewrite_rules');
- function mmp_rewrite_rules($rules) {
- //TIPOLOGIA
- $tipologie = get_terms('auto-merk', array(
- 'hide_empty' => false
- ));
- foreach ($tipologie as $tipologia) {
- $slugs_tipologie [] = $tipologia->slug;
- }
- $num_tipologie = count($slugs_tipologie);
- $regex_tipologia = '';
- if( $num_tipologie > 0) {
- for( $i=0; $i < $num_tipologie-1; $i++ ) {
- $regex_tipologia .= $slugs_tipologie[$i] .'|';
- }
- $regex_tipologia .= array_pop($slugs_tipologie);
- }
- $ee1 = merk."/($regex_tipologia)/(.+)/(.+)/(.+)/(.+)/?$";
- $ee2 = merk."/($regex_tipologia)/?$";
- $ee3 = merk."/($regex_tipologia)/(.+)/?$";
- $ee4 = merk."/(.+)/?$";
- $newRules = array();
- $newRules[$ee1] = 'index.php?auto=$matches[5]';
- $newRules[$ee2] = 'index.php?auto-merk=$matches[1]';
- $newRules[$ee3] = 'index.php?auto-merk=$matches[1]&auto-bouwjaar=$matches[2]';
- $newRules[$ee4] = 'index.php?auto-bouwjaar=$matches[1]';
- return array_merge($newRules, $rules);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement