【发布时间】:2015-08-06 21:00:37
【问题描述】:
我创建了自定义帖子类型:书籍 并为此创建了重写规则:
$new_rules[ '([^/]+)$' ] = 'index.php?post_type=book&name=$matches[1]';
我工作正常!图书网址是: http://domain.com/bookname/
但是当我打开页面(默认页面 posty 类型)时,我收到 404 错误。
【问题讨论】:
标签: wordpress
我创建了自定义帖子类型:书籍 并为此创建了重写规则:
$new_rules[ '([^/]+)$' ] = 'index.php?post_type=book&name=$matches[1]';
我工作正常!图书网址是: http://domain.com/bookname/
但是当我打开页面(默认页面 posty 类型)时,我收到 404 错误。
【问题讨论】:
标签: wordpress
在注册您的自定义帖子类型时使用 with_front 设置为 false 将从您的自定义帖子类型中删除 slug,
'rewrite' => array('slug' => 'book', 'with_front' => false),
示例:使用rewrite 规则注册您的帖子类型时。参见register_post_type手册
add_action('init', 'codex_book_init');
function codex_book_init()
{
$labels = array(
'name' => _x('Books', 'post type general name', 'your-plugin-textdomain'),
'singular_name' => _x('Books', 'post type singular name', 'your-plugin-textdomain'),
'menu_name' => _x('Books', 'admin menu', 'your-plugin-textdomain'),
'name_admin_bar' => _x('Books', 'add new on admin bar', 'your-plugin-textdomain'),
'add_new' => _x('Add New', 'book', 'your-plugin-textdomain'),
'add_new_item' => __('Add New Books', 'your-plugin-textdomain'),
'new_item' => __('New Books', 'your-plugin-textdomain'),
'edit_item' => __('Edit Books', 'your-plugin-textdomain'),
'view_item' => __('View Books', 'your-plugin-textdomain'),
'all_items' => __('All Books', 'your-plugin-textdomain'),
'search_items' => __('Search Books', 'your-plugin-textdomain'),
'parent_item_colon' => __('Parent Books:', 'your-plugin-textdomain'),
'not_found' => __('No books found.', 'your-plugin-textdomain'),
'not_found_in_trash' => __('No books found in Trash.', 'your-plugin-textdomain')
);
$args = array(
'labels' => $labels,
'description' => __('Description.', 'your-plugin-textdomain'),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array('slug' => 'book', 'with_front' => false),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments')
);
register_post_type('book', $args);
}
【讨论】:
USE Cases 测试了上面的代码,我在已经注册的帖子上遇到了同样的问题,但是使用with_front=>false 用新的测试帖子类型修复了。
要解决这个问题,请在您的 register_post_type 调用中启用带有 'with_front' => true 参数的 url base,这样您的 url 将看起来像 domain.com/book/book_name
【讨论】: