我有一个自定义的贴子类型,记录诊所提供的治疗方法。这个位于网站的化妆品部分(site.com/cosmetics/
).
筛选器已成功转换\\%type\\
对于permalinks。这就是我想要的结构:site.com/cosmetics/treatments
site.com/cosmetics/treatments/[type]/
site.com/cosmetics/treatments/[type]/[treatment]
我已经试着理解这里的许多答案了。我的代码中有什么想法或明显的缺陷吗?我已经每次都在冲洗permalinks了。
site.com/cosmetics/treatments/[type]
工作正常。治疗页面本身从管理面板链接到以下内容:site.com/[type]/[treatment]
这是404错误。
UPDATE
我重新排列了一些代码,所以我在这里更改了代码以反映这一点。问题仍然没有改变。
插件代码:
class NSCTreatments {
/**
* Constructor for taxonomy and post type initialisation
*/
function __construct(){
add_action( \'init\', array( $this, \'register_custom_taxonomies\') ); // this MUST go first
add_action( \'init\', array( $this, \'register_custom_post_type\' ) );
add_filter(\'post_link\', \'treatment_permalink\', 10, 3);
add_filter(\'post_type_link\', \'treatment_permalink\', 10, 3);
function treatment_permalink($permalink, $post_id, $leavename) {
if (strpos($permalink, \'%type%\') === FALSE) return $permalink;
// Get post
$post = get_post($post_id);
if (!$post) return $permalink;
// Get taxonomy terms
$terms = wp_get_object_terms($post->ID, \'treatment-type\');
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $taxonomy_slug = $terms[0]->slug;
else $taxonomy_slug = \'no-type\';
return str_replace(\'%type%\', $taxonomy_slug, $permalink);
}
}
function register_custom_taxonomies (){
$labels = array(
\'name\' => _x( \'Treatment Types\', \'taxonomy general name\' ),
\'singular_name\' => _x( \'Treatment Type\', \'taxonomy singular name\' ),
\'search_items\' => __( \'Search Treatment Types\' ),
\'all_items\' => __( \'All Treatment Types\' ),
\'parent_item\' => __( \'Parent Treatment Types\' ),
\'parent_item_colon\' => __( \'Parent Treatment Types:\' ),
\'edit_item\' => __( \'Edit Treatment Type\' ),
\'update_item\' => __( \'Update Treatment Type\' ),
\'add_new_item\' => __( \'Add New Treatment Type\' ),
\'new_item_name\' => __( \'New Treatment Type Name\' ),
\'menu_name\' => __( \'Treatment Type\' ),
);
$args = array(
\'hierarchical\' => false,
\'labels\' => $labels,
\'show_ui\' => true,
\'show_admin_column\' => false,
\'query_var\' => true,
\'rewrite\' => array( \'slug\' => \'cosmetics/treatments\', \'with_front\' => false ),
);
register_taxonomy( \'treatment-type\', array( \'treatment\' ), $args );
}
/**
* Registers a Custom Post Type called Treatment
*/
function register_custom_post_type() {
register_post_type( \'treatment\', array(
\'labels\' => array(
\'name\' => _x( \'Treatments\', \'post type general name\', \'nsc-treatments\' ),
\'singular_name\' => _x( \'Treatment\', \'post type singular name\', \'nsc-treatments\' ),
\'menu_name\' => _x( \'Treatments\', \'admin menu\', \'nsc-treatments\' ),
\'name_admin_bar\' => _x( \'Treatment\', \'add new on admin bar\', \'nsc-treatments\' ),
\'add_new\' => _x( \'Add New\', \'treatment\', \'nsc-treatments\' ),
\'add_new_item\' => __( \'Add New Treatment\', \'nsc-treatments\' ),
\'new_item\' => __( \'New Treatment\', \'nsc-treatments\' ),
\'edit_item\' => __( \'Edit Treatment\', \'nsc-treatments\' ),
\'view_item\' => __( \'View Treatment\', \'nsc-treatments\' ),
\'all_items\' => __( \'All Treatments\', \'nsc-treatments\' ),
\'search_items\' => __( \'Search Treatments\', \'nsc-treatments\' ),
\'parent_item_colon\' => __( \'Parent Treatment:\', \'nsc-treatments\' ),
\'not_found\' => __( \'No treatment found.\', \'nsc-treatments\' ),
\'not_found_in_trash\' => __( \'No treatment found in Trash.\', \'nsc-treatments\' ),
),
// Frontend
\'has_archive\' => \'cosmetics/treatments\',
\'public\' => true,
\'publicly_queryable\' => true,
\'rewrite\' => array(\'slug\' => \'%type%\', \'with_front\' => false),
// Admin
\'capability_type\' => \'post\',
\'menu_icon\' => \'dashicons-screenoptions\',
\'menu_position\' => 20,
\'query_var\' => true,
\'show_in_menu\' => true,
\'show_ui\' => true,
\'supports\' => array(
\'title\',
\'author\',
),
) );
}
}