0

I have a custom taxonomy which behaves similar to the post type as a category behaves to a the default posts (on the grounds of /%category%/%postname%/ permalink structure) so that the posts in the custom post types are displayed as www.example.com/custom-post-type/custom-taxonomy-name/post-name..

Everything thing works fine except for one element.. the permalink/slug is not correct. The url at this moment is like: www.example.com/behandeling/behandeling_categorie/post-name

The middle part needs be different. It needs to have the term.. not the general taxonomy_name..

This is my code:

// Register Custom Post Type
function ats_kliniek_custom_post_type() {

$labels = array(
    'name'                  => _x( 'behandelingen', 'Post Type General Name', 'text_domain' ),
    'singular_name'         => _x( 'behandeling', 'Post Type Singular Name', 'text_domain' ),
    'menu_name'             => __( 'Behandelingen', 'text_domain' ),
    'name_admin_bar'        => __( 'Behandeling type', 'text_domain' ),
    'archives'              => __( 'behandeling archieven', 'text_domain' ),
    'attributes'            => __( 'behandeling atributen', 'text_domain' ),
    'parent_item_colon'     => __( 'Parent behandeling', 'text_domain' ),
    'all_items'             => __( 'Alle behandelingen', 'text_domain' ),
    'add_new_item'          => __( 'Voeg nieuwe behandeling toe', 'text_domain' ),
    'add_new'               => __( 'Voeg nieuwe behandeling toe', 'text_domain' ),
    'new_item'              => __( 'Nieuwe behandeling', 'text_domain' ),
    'edit_item'             => __( 'Bewerkt behandeling', 'text_domain' ),
    'update_item'           => __( 'Werk behandeling bij', 'text_domain' ),
    'view_item'             => __( 'Bekijk behandeling', 'text_domain' ),
    'view_items'            => __( 'Bekijk behandelingen', 'text_domain' ),
    'search_items'          => __( 'Zoek behandelingen', 'text_domain' ),
    'not_found'             => __( 'Behandeling niet gevonden', 'text_domain' ),
    'not_found_in_trash'    => __( 'Geen behandelingen gevonden in prullenbak', 'text_domain' ),
    'featured_image'        => __( 'Uitgelichte afbeelding', 'text_domain' ),
    'set_featured_image'    => __( 'Stel uitgelichte behandeling in', 'text_domain' ),
    'remove_featured_image' => __( 'Verwijder uitgelichte behandeling', 'text_domain' ),
    'use_featured_image'    => __( 'Gebruik als uitgelichte behandeling', 'text_domain' ),
    'insert_into_item'      => __( 'Plaats in behandeling', 'text_domain' ),
    'uploaded_to_this_item' => __( 'Uploaded to this item', 'text_domain' ),
    'items_list'            => __( 'Behandelingen lijst', 'text_domain' ),
    'items_list_navigation' => __( 'Behandelingen lijst navigatie', 'text_domain' ),
    'filter_items_list'     => __( 'Filter behandelingen lijst', 'text_domain' ),
);
$rewrite = array(
    'slug'                  => 'behandelingen/behandeling_categorie',
    'with_front'            => false,
    'pages'                 => true,
    'feeds'                 => true,
);
$args = array(
    'label'                 => __( 'behandeling', 'text_domain' ),
    'description'           => __( 'behandeling custom post type', 'text_domain' ),
    'labels'                => $labels,
    'supports'              => array( 'title', 'editor', 'thumbnail', 'custom-fields', ),
    'taxonomies'            => array( 'behandeling_categorie' ),
    'hierarchical'          => true,
    'public'                => true,
    'show_ui'               => true,
    'show_in_menu'          => true,
    'menu_position'         => 5,
    'show_in_admin_bar'     => false,
    'show_in_nav_menus'     => true,
    'can_export'            => true,
    'has_archive'           => 'behandelingen',
    'exclude_from_search'   => false,
    'publicly_queryable'    => true,
    'rewrite'               => $rewrite,
    'capability_type'       => 'page',
);
register_post_type( 'behandelingen', $args );

}
add_action( 'init', 'ats_kliniek_custom_post_type', 0 );

// Register Custom Taxonomy
function ats_kliniek_custom_taxonomy() {

$labels = array(
    'name'                       => _x( 'behandeling categorieën', 'Taxonomy General Name', 'text_domain' ),
    'singular_name'              => _x( 'behandeling categorie', 'Taxonomy Singular Name', 'text_domain' ),
    'menu_name'                  => __( 'Behandeling categorie', 'text_domain' ),
    'all_items'                  => __( 'Alle behandeling categorieën', 'text_domain' ),
    'parent_item'                => __( 'Parent categorie', 'text_domain' ),
    'parent_item_colon'          => __( 'Parent categorie:', 'text_domain' ),
    'new_item_name'              => __( 'Nieuwe behandeling categorie', 'text_domain' ),
    'add_new_item'               => __( 'Voeg nieuwe behandeling categorie toe', 'text_domain' ),
    'edit_item'                  => __( 'Bewerk categorie', 'text_domain' ),
    'update_item'                => __( 'Update categorie', 'text_domain' ),
    'view_item'                  => __( 'Bekijk categorie', 'text_domain' ),
    'separate_items_with_commas' => __( 'Separate items with commas', 'text_domain' ),
    'add_or_remove_items'        => __( 'Voeg of verwijderen categorieën', 'text_domain' ),
    'choose_from_most_used'      => __( 'Kies van meest gebruikte', 'text_domain' ),
    'popular_items'              => __( 'Populaire categorieën', 'text_domain' ),
    'search_items'               => __( 'Zoeken behandeling categorieën', 'text_domain' ),
    'not_found'                  => __( 'Geen behandeling categorieën gevonden', 'text_domain' ),
    'no_terms'                   => __( 'Geen behandeling categorieën', 'text_domain' ),
    'items_list'                 => __( 'Geen behandeling categorieën lijst', 'text_domain' ),
    'items_list_navigation'      => __( 'behandeling categorieën lijst navigatie', 'text_domain' ),
);
$rewrite = array(
    'slug'                       => 'behandelingen',
    'with_front'                 => false,
    'hierarchical'               => true,
);
$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( 'behandeling_categorie', array( 'behandelingen' ), $args );

}
add_action( 'init', 'ats_kliniek_custom_taxonomy', 0 );


function ats_kliniek_filter_post_type_link($link, $post)
{
if ($post->post_type != 'behandelingen')
    return $link;

if ($cats = get_the_terms($post->ID, 'behandeling_categorie'))
    $link = str_replace('%behandeling_categorie%', array_pop($cats)->slug, $link);
return $link;
}
add_filter('post_type_link', 'ats_kliniek_filter_post_type_link', 10, 2);

Do you guys see what i am doing wrong?

2

Well, from what I can see, the reason for your wrong middle part is that it is hard coded in the slug?

'slug'                  => 'behandelingen/behandeling_categorie',
  • Thx.. i've changed it to: 'behandelingen/%behandeling_categorie%' – Bram Huisman Mar 1 '17 at 8:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.