我需要我的帖子固定链接包含自定义分类值type
,如下所示:
http://staging.mysite.com/article/post-title
article
是该帖子的type
分类值。我已经让这个工作了,但我遇到的问题是现在我所有网站的页面都是404。我的自定义帖子类型和普通帖子网址按预期工作,它只是被破坏的页面网址。以下是导致页面网址问题的代码:
// Type taxonomy (no issues here)
function create_type_taxonomy() {
register_taxonomy(
'type',
'post',
array(
'labels' => array(
'name' => 'Type',
'add_new_item' => 'Add New Type',
'new_item_name' => 'New Type'
),
'show_ui' => true,
'show_tagcloud' => false,
'hierarchical' => false,
'rewrite' => array(
'slug' => 'type',
'with_front' => true
),
)
);
}
add_action( 'init', 'create_type_taxonomy', 0 );
add_action( 'init', 'st_default_post_type', 1 );
// Re-register built-in posts with a custom rewrite rule
function st_default_post_type() {
register_post_type( 'post', array(
'labels' => array(
'name_admin_bar' => _x( 'Post', 'add new on admin bar' ),
),
'public' => true,
'_builtin' => false,
'_edit_link' => 'post.php?post=%d',
'capability_type' => 'post',
'map_meta_cap' => true,
'hierarchical' => false,
'rewrite' => array( 'slug' => '%type%', 'with_front' => false ), // custom rewrite rule
'query_var' => false,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'post-formats' ),
) );
}
add_filter('post_type_link', 'st_posts_permalink_structure', 10, 4);
// Replace custom rewrite rule on posts (%type%) with the taxonomy value
function st_posts_permalink_structure($post_link, $post, $leavename, $sample){
if ($post->post_type != 'post') {
var_dump($post->post_type);
return $post_link;
}
else{
if (strpos($post_link, '%type%') === FALSE){
return $post_link;
}
$post = get_post($post);
if (!$post){
return $post_link;
}
$terms = wp_get_object_terms($post->ID, 'type');
if ( !is_wp_error($terms) && !empty($terms) && is_object($terms[0]) ){
$taxonomy_slug = $terms[0]->slug;
}
else{
$taxonomy_slug = 'type';
}
return str_replace('%type%', $taxonomy_slug, $post_link);
}
}
希望另一组眼睛可能会捕获导致页面永久链接为404的内容。我已经尝试将管理中的永久链接设置更改为/%type%/%postname%/
,但这有同样的问题。我在这里发现了几个其他问题,看起来和我有同样的问题,但没有一个问题得到解答: