自定义帖子类型永久链接返回 404
通过搜索支持论坛和博客,我发现除了我之外,还有相当多的人在自定义帖子类型和永久链接方面遇到困难。
我的自定义帖子类型(日记)注册正常,并且在管理端一切都显示正常。如果您在前端搜索它,它就会出现在搜索结果中。然而,当我点击日记帖子时,我收到了 404 错误。
我的代码:
register_post_type( 'diary_post',
array(
'labels' => array(
'name' => __( 'Diary Post' ),
'singular_name' => __( 'Diary Post' ),
'add_new' => _x('Add New'),
'add_new_item' => __('Add New Diary Post'),
'edit_item' => __('Edit Diary Post'),
'new_item' => __('New Diary Post'),
'view_item' => __('View Diary Post'),
'search_items' => __('Search Diary Posts'),
'not_found' => __('No Diary posts found'),
'not_found_in_trash' => __('No Diary posts found in Trash'),
'parent_item_colon' => ''
),
'description' => __( 'Posts to appear on the Diary page' ),
'public' => true,
'publicly_queryable' => false,
'exclude_from_search' => false,
'query_var' => true,
'menu_position' => 4,
'supports' => array('title','editor','author','excerpt','thumbnail', 'custom-fields','comments','trackbacks','revisions'),
'taxonomies' => array( 'diary_post_type','post_tag'),
'has_archive' => true,
'rewrite' => array( 'slug' => 'diary','with_front' => false)
)
);
我已经尝试了各种帖子中建议的以下解决方案,但没有成功:
- 重新保存我的永久链接将
- “rewrite”更改为“true”两个
- “with_front”=> false 和
- 'with_front' => true 添加
- flush_rewrite_rules(); 检查后,
- 在register_post_type
- 没有名为“diary”的页面或帖子
即使我尝试通过默认的永久链接结构访问帖子,例如 http://localhost/?diary_post=my-title-here 我没有成功。
我的网站永久链接结构当前为 /%year%/%postname%
- 将其更改为默认设置也没有帮助。
有什么线索吗?我已经无计可施了。
From trawling the support forum and blogs I see that a fair few people other than myself are having difficulties with custom post types and permalinks.
My custom post type (diary) registers ok and all appears fine on the admin side. If you search for it on the front end it appears in the search results. However I get a 404 when I click through to the diary post.
My code:
register_post_type( 'diary_post',
array(
'labels' => array(
'name' => __( 'Diary Post' ),
'singular_name' => __( 'Diary Post' ),
'add_new' => _x('Add New'),
'add_new_item' => __('Add New Diary Post'),
'edit_item' => __('Edit Diary Post'),
'new_item' => __('New Diary Post'),
'view_item' => __('View Diary Post'),
'search_items' => __('Search Diary Posts'),
'not_found' => __('No Diary posts found'),
'not_found_in_trash' => __('No Diary posts found in Trash'),
'parent_item_colon' => ''
),
'description' => __( 'Posts to appear on the Diary page' ),
'public' => true,
'publicly_queryable' => false,
'exclude_from_search' => false,
'query_var' => true,
'menu_position' => 4,
'supports' => array('title','editor','author','excerpt','thumbnail', 'custom-fields','comments','trackbacks','revisions'),
'taxonomies' => array( 'diary_post_type','post_tag'),
'has_archive' => true,
'rewrite' => array( 'slug' => 'diary','with_front' => false)
)
);
I have tried the following suggested solutions from various posts without any success:
- Re-saving my permalinks Changing
- 'rewrite' to just 'true' Both
- 'with_front' => false and
- 'with_front' => true Adding
- flush_rewrite_rules(); after
- register_post_type
- Checking there are no pages or posts named 'diary'
Even if I try to access a post via the default permalink structure such as http://localhost/?diary_post=my-title-here I have no success.
My permalink structure for the site is currently /%year%/%postname%
- changing this to the default setting doesn't help either.
Any clues? At my wit's end here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
publicly_queryable
需要为 true,但如果您设置public
,则可能不需要它(或exclude_from_search
)。请参阅代码:http://codex.wordpress.org/Function_Reference/register_post_type#Argumentspublicly_queryable
needs be true, but you probably don't need it (orexclude_from_search
) if you are settingpublic
. See the codex: http://codex.wordpress.org/Function_Reference/register_post_type#Arguments这是另一个答案,可以帮助可能遇到与我相同问题的人...
如果您最近创建了新的 CPT,您可能需要进入设置 >固定链接,然后再次单击保存更改。这允许任何新的重写规则生效。
Here's another answer to help people that may have the same problem as I did...
If you've recently created the new CPT, you may need to go into Settings > Permalinks and click Save Changes again. This allows any new rewrite rules to take effect.
您应该将“_builtin”属性添加到参数中。
You should add "_builtin" property to arguments.