制作自定义文章类型分层固定链接平板

问题描述:

所以基本上我一直在试图让我的永久链接看起来像这样:制作自定义文章类型分层固定链接平板

/食谱/帖子ID/postname

而不是什么有可能成为这个潜力:

/食谱/ postparent/postparent/postparent/postparent/postname

用户必须创建前端是其他职位的孩子职位的能力。这可以继续下去,我不希望永久链接变得疯狂。

我能够使用下面的代码从永久链接中删除所有父邮件名。但是,如果某人创建了一个已存在的帖子名称,则这不起作用。

我想能够更改固定链接以包含帖子ID,所以这不会发生,但我无法弄清楚。我感谢任何帮助!

下面是代码:

function Recipes() { 

$labels = array(
'name'     => _x('Recipes', 'Post Type Recipes', 'recipes'), 
'singular_name'   => _x('Recipe', 'Post Type Singular Name', 'Recipe'), 
'menu_name'    => __('Recipes', 'recipes'), 
'name_admin_bar'  => __('Recipes', 'recipes'), 
'archives'    => __('Recipes Archives', 'recipes'), 
'parent_item_colon'  => __('Parent Recipe', 'recipes'), 
'all_items'    => __('All Recipes', 'recipes'), 
'add_new_item'   => __('Add New Recipe', 'recipes'), 
'add_new'    => __('Add Recipe', 'recipes'), 
'new_item'    => __('New Recipe', 'recipes'), 
'edit_item'    => __('Edit Recipe', 'recipes'), 
'update_item'   => __('Update Recipe', 'recipes'), 
'view_item'    => __('View Recipe', 'recipes'), 
'search_items'   => __('Search Recipes', 'recipes'), 
); 
$args = array(
'label'     => __('Recipes', 'Recipes'), 
'description'   => __('Recipes', 'recipes'), 
'labels'    => $labels, 
'supports' => array(
    'title', 
    'thumbnail', 
    'comments', 
    'editor', 
    'revisions'), 
'taxonomies'   => array('category', 'recipes-tag'), 
'hierarchical'   => true, 
'public'    => true, 
'show_ui'    => true, 
'show_in_menu'   => true, 
'menu_position'   => 5, 
'menu_icon'   => 'dashicons-editor-ul', 
'show_in_admin_bar'  => true, 
'show_in_nav_menus'  => true, 
'can_export'   => true, 
'has_archive'   => true, 
'exclude_from_search' => false, 
'publicly_queryable' => true, 
'capability_type'  => 'page', 
'show_in_rest'  => true, 
'rest_controller_class' => 'WP_REST_Posts_Controller', 
'rewrite'    => array('slug' => 'recipes'), 

); 
register_post_type('recipes', $args); 

add_rewrite_rule(
     '^recipes/([^/]+)/?$', 
     'index.php?post_type=recipes&name=$matches[1]', 
     'top' 
    ); 

} 

add_action('init', 'Recipes', 0); 


function bvt_recipes_flatten_hierarchies($post_link, $post) { 
    if ('recipes' != $post->post_type) { 
     return $post_link; 
    } 
    $uri = ''; 
    foreach ($post->ancestors as $parent) { 
     $uri = get_post($parent)->post_name . "/" . $uri; 
    } 

    return str_replace($uri, '', $post_link); 
} 
add_filter('post_type_link', 'bvt_recipes_flatten_hierarchies', 10, 2); 

UPDATE

我想通了。如果有其他人遇到这个问题,以下是这些变化。

function Recipes() { 

$labels = array(
'name'     => _x('Recipes', 'Post Type Recipes', 'recipes'), 
'singular_name'   => _x('Recipe', 'Post Type Singular Name', 'Recipe'), 
'menu_name'    => __('Recipes', 'recipes'), 
'name_admin_bar'  => __('Recipes', 'recipes'), 
'archives'    => __('Recipes Archives', 'recipes'), 
'parent_item_colon'  => __('Parent Recipe', 'recipes'), 
'all_items'    => __('All Recipes', 'recipes'), 
'add_new_item'   => __('Add New Recipe', 'recipes'), 
'add_new'    => __('Add Recipe', 'recipes'), 
'new_item'    => __('New Recipe', 'recipes'), 
'edit_item'    => __('Edit Recipe', 'recipes'), 
'update_item'   => __('Update Recipe', 'recipes'), 
'view_item'    => __('View Recipe', 'recipes'), 
'search_items'   => __('Search Recipes', 'recipes'), 
); 
$args = array(
'label'     => __('Recipes', 'Recipes'), 
'description'   => __('Recipes', 'recipes'), 
'labels'    => $labels, 
'supports' => array(
    'title', 
    'thumbnail', 
    'comments', 
    'editor', 
    'revisions'), 
'taxonomies'   => array('category', 'recipes-tag'), 
'hierarchical'   => true, 
'public'    => true, 
'show_ui'    => true, 
'show_in_menu'   => true, 
'menu_position'   => 5, 
'menu_icon'   => 'dashicons-editor-ul', 
'show_in_admin_bar'  => true, 
'show_in_nav_menus'  => true, 
'can_export'   => true, 
'has_archive'   => true, 
'exclude_from_search' => false, 
'publicly_queryable' => true, 
'capability_type'  => 'page', 
'show_in_rest'  => true, 
'rest_controller_class' => 'WP_REST_Posts_Controller', 
// 'rewrite'    => array('slug' => 'recipes'), 

); 
register_post_type('recipes', $args); 

add_rewrite_rule(
     'recipes/([a-z-]+)/([0-9]+)?$', 
     'index.php?post_type=recipes&name=$matches[1]&p=$matches[2]', 
     'top'); 

} 

add_action('init', 'Recipes', 0); 



function change_post_type_link($link, $post = 0){ 
    if ($post->post_type == 'recipes'){ 
     return home_url('recipes/'. $post->post_name .'/'. $post->ID); 
    } else { 
     return $link; 
    } 
} 


add_filter('post_type_link', 'change_post_type_link', 10, 2);