自定义帖子类型永久链接返回 404

发布于 2024-11-05 06:46:35 字数 1837 浏览 0 评论 0原文

通过搜索支持论坛和博客,我发现除了我之外,还有相当多的人在自定义帖子类型和永久链接方面遇到困难。

我的自定义帖子类型(日记)注册正常,并且在管理端一切都显示正常。如果您在前端搜索它,它就会出现在搜索结果中。然而,当我点击日记帖子时,我收到了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一绘本一梦想 2024-11-12 06:46:35

publicly_queryable 需要为 true,但如果您设置 public,则可能不需要它(或 exclude_from_search)。请参阅代码:http://codex.wordpress.org/Function_Reference/register_post_type#Arguments

publicly_queryable needs be true, but you probably don't need it (or exclude_from_search) if you are setting public. See the codex: http://codex.wordpress.org/Function_Reference/register_post_type#Arguments

夜还是长夜 2024-11-12 06:46:35

这是另一个答案,可以帮助可能遇到与我相同问题的人...

如果您最近创建了新的 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.

青春如此纠结 2024-11-12 06:46:35

您应该将“_builtin”属性添加到参数中。

function rw_portfolio_register(){
    $args = array(
        'label' => __('Portfolio'),
        'singular_label' => __('Portfolio'),
        'public' => true,
        'show_ui' => true,
        '_builtin' => false,
        'capability_type' => 'post',
        'hierarchical' => false,
        'rewrite' => array("slug" => "project"),
        'supports' => array('title', 'editor')//Boxes will be showed in the panel
       );

    register_post_type( 'rw_portfolio' , $args );
}

You should add "_builtin" property to arguments.

function rw_portfolio_register(){
    $args = array(
        'label' => __('Portfolio'),
        'singular_label' => __('Portfolio'),
        'public' => true,
        'show_ui' => true,
        '_builtin' => false,
        'capability_type' => 'post',
        'hierarchical' => false,
        'rewrite' => array("slug" => "project"),
        'supports' => array('title', 'editor')//Boxes will be showed in the panel
       );

    register_post_type( 'rw_portfolio' , $args );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文