【发布时间】:2020-11-26 11:01:35
【问题描述】:
WordPress 自定义帖子类型链接已更改但返回 404。
我已经创建了一个这样的自定义帖子类型,
function create_notes_custom() {
register_post_type( 'coaches_resources',
array(
'labels' => array(
'name' => __( 'Coaches Resources' ),
'singular_name' => __( 'Coaches Resource' ),
'add_new' => __( 'Add New' ),
'add_new_item' => __( 'Add New Resource' ),
'edit' => __( 'Edit' ),
'edit_item' => __( 'Edit Resource' ),
'new_item' => __( 'New Resource' ),
'view' => __( 'View' ),
'view_item' => __( 'View Resource' ),
'search_items' => __( 'Search Resources' ),
'not_found' => __( 'No resources found' ),
'not_found_in_trash' => __( 'No resource found in Trash' ),
'parent' => __( 'Parent Resource' )
),
'public' => true,
'menu_position' => 5,
'supports' => array( 'title' ),
'taxonomies' => array( 'resource_chapter' ),
'rewrite' => array( 'slug' => 'resource', 'with_front' => true ),
'has_archive' => false,
'hierarchical' => false
)
);
}
add_action('init', 'create_notes_custom');
网址更改为,
function change_link( $permalink, $post ) {
if( $post->post_type == 'coaches_resources' ) {
$resource_terms = get_the_terms( $post, 'resource_chapter' );
$term_slug = '';
if( ! empty( $resource_terms ) ) {
foreach ( $resource_terms as $term ) {
if (! empty($term->slug)) {
// The featured resource will have another category which is the main one
if( $term->slug == 'featured' ) {
continue;
}
$term_slug = $term->slug;
break;
}
}
}
$permalink = get_home_url() ."/notebook/" . $term_slug . '/' . $post->post_name;
}
return $permalink;
}
add_filter('post_type_link', "change_link", 10, 2);
网址从 http://example.com/resource/my-first-note/ 至 http://example.com/notebook/note-category/my-first-note
但它返回404。
【问题讨论】:
-
我认为您还需要在过滤后重写它们。检查这个在
filterwordpress.stackexchange.com/questions/33551/…之后也与init挂钩的解决方案@
标签: wordpress custom-post-type