我创建了一个自定义帖子类型,自定义分类和自定义永久链接,但是出了点问题。我想我忘了几行。
domain / workbench-gallery / 是404
domain / workbench-gallery / custom-category 登陆到archive.php
domain / workbench-gallery / custom-category / post-title 登陆到单一分类法.php
我想要的是:
这是我的代码:
注册自定义帖子类型
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array('slug' => 'workbench-gallery/%fsgallery_category%', 'with_front' => false),
'capability_type' => 'post',
'has_archive' => false,
'hierarchical' => true,
'menu_position' => null,
'rest_base' => 'fsgallery',
'menu_icon' => 'dashicons-format-gallery',
'supports' => array( 'title', 'permalink', <br/>'gallery_meta_box', 'editor', 'author', 'thumbnail', 'excerpt' ),
);
register_post_type( 'fsgallery', $args );
flush_rewrite_rules();
注册自定义分类类型
$labels = array(
'name' => _x( 'Gallery categories', 'taxonomy general name', 'fsgallery' ),
'singular_name' => _x( 'Gategory for gallery', 'taxonomy singular name', 'fsgallery' ));
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'slug' => 'fsgallery_category',
'query_var' => true,
'rewrite' => array( 'slug' => 'workbench-gallery', 'with_front' => false ),
);
register_taxonomy( 'fsgallery_category', array( 'fsgallery' ), $args );
替换并显示永久链接
function wpa_show_permalinks( $post_link, $post ){
if ( is_object( $post ) && $post->post_type == 'fsgallery' ){
$terms = wp_get_object_terms( $post->ID, 'fsgallery_category' );
if( $terms ){
return str_replace( '%fsgallery_category%' , $terms[0]->slug , $post_link );
}
}
return $post_link;
}
add_filter( 'post_type_link', 'wpa_show_permalinks', 1, 2 );