特定于自定义发布类型的自定义分类-重写URL

时间:2017-02-24 作者:Bram Huisman

我有一个自定义分类法,其行为类似于帖子类型,因为类别的行为类似于默认帖子(基于/%category%/%postname%/permalink结构),因此自定义帖子类型中的帖子显示为www.example。com/自定义帖子类型/自定义分类名称/帖子名称。。

除了一个元素外,一切都很好。。永久对齐/缓动不正确。此时的url类似于:www.example。com/behandeling/behandeling\\U类别/帖子名称

中间部分需要不同。它需要有一个术语。。不是一般的分类法名称。。

这是我的代码:

// 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);
你们知道我做错了什么吗?

1 个回复
最合适的回答,由SO网友:Svartbaard 整理而成

好吧,从我所看到的,你的中间部分错误的原因是它在slug中是硬编码的?

\'slug\'                  => \'behandelingen/behandeling_categorie\',

相关推荐