使自定义帖子类型分层永久链接平坦

时间:2017-05-27 21:55:56

标签: wordpress custom-post-type slug

所以基本上我一直试图让我的永久链接看起来像这样:

/食谱/帖子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 );

1 个答案:

答案 0 :(得分:0)

更新

我明白了。以下是其他人遇到此问题时的更改。

String json = "{'shopper': {'Id': '4973860941232342', 'Context': {'CollapseOrderItems': false, 'IsTest': false } }, 'SelfIdentifiersData': {'SelfIdentifierData': [{'SelfIdentifierType': {'SelfIdentifierType': '111'} }, {'SelfIdentifierType': {'SelfIdentifierType': '2222'} } ] } }";

Configuration conf = Configuration.defaultConfiguration();
List<String> jsonPaths = JsonPath.using(conf).parse(json).read("$");

for (String path : jsonPaths) {
    System.out.println(path);
}