自定义帖子类型的WordPress页面父级

时间:2014-09-30 18:38:37

标签: php wordpress custom-post-type

我尝试将自定义帖子类型staff设置为页面about-us的子级,例如:

example.com/staff/john-doe转到example.com/about-us/john-doe

我目前在Google和Stack上发布的所有帖子均无济于事,我能找到的壁橱解决方案是将下面的内容添加到我的functions.php,这会添加一个元框,让我可以设置个人工作人员的父页面。

add_action('admin_menu', function() { remove_meta_box('pageparentdiv', 'chapter', 'normal');});
add_action('add_meta_boxes', function() { add_meta_box('chapter-parent', 'Part', 

'chapter_attributes_meta_box', 'chapter', 'side', 'high');});
  function chapter_attributes_meta_box($post) {
    $post_type_object = get_post_type_object($post->post_type);
    if ( $post_type_object->hierarchical ) {
      $pages = wp_dropdown_pages(array('post_type' => 'part', 'selected' => $post->post_parent, 'name' => 'parent_id', 'show_option_none' => __('(no parent)'), 'sort_column'=> 'menu_order, post_title', 'echo' => 0));
      if ( ! empty($pages) ) {
        echo $pages;
      } // end empty pages check
    } // end hierarchical check.
  }

然而,当将其与'rewrite' => array('slug' => 'about-us'),结合使用时,它似乎导致example.com/about-us/about-us/john-doe而没有它example.com/staff/about-us/john-doe也无法正常工作!

首先,我想知道上面的问题是否有解决方案,其次是有更好的方法来实现这一点,并且可能无需为每个帖子手动选择父页面,而是整个自定义邮件类型设置为页面的子项?

如果我在此方面取得任何进展,我一定会更新!

不要再混淆你,但尝试这样做的动机是导航相关,所以我的菜单系统会自动调整。如果这不是一个问题我只会使用重写并有一个自定义菜单,我需要每次移动或添加页面时更新!当您有多个CPT时,这显然不太理想。

1 个答案:

答案 0 :(得分:0)

这可能与您的register_post_type()调用有关。您的重写可能需要如下所示:

'rewrite' => array('slug' => 'about-us', 'with_front' => false )

" with_front"部分应删除您当前在任何帖子前面的任何内容。例如,如果你的永久链接部分中你正在使用工作人员预先填写所有帖子(看起来你正在做的事情,虽然它只是猜测,因为我无法看到你的设置),with_front会忽略它。 / p>

使用此解决方案,您也不会需要额外的元框功能。

请勿忘记在此更改后访问固定链接页面以确保永久链接已刷新且您没有获得401 ...

相关问题