如何重写url?post_type=WordPress

如何重写url?post_type=WordPress,wordpress,Wordpress,我在WordPress中重写url自定义帖子类型时有点问题 我有这个链接:www.domain.com/?post\u type=review\u smartphone 我想更改为:www.domain.com/smartphone 如何制作,请帮忙。我尝试了这个解决方案:但我不明白如何实现它。用这段代码修复了这个问题 /** * Re-write post type urls */ function rewrite_post_type_init() { // semua post ty

我在WordPress中重写url自定义帖子类型时有点问题

我有这个链接:www.domain.com/?post\u type=review\u smartphone 我想更改为:www.domain.com/smartphone

如何制作,请帮忙。我尝试了这个解决方案:但我不明白如何实现它。

用这段代码修复了这个问题

/**
* Re-write post type urls
*/
function rewrite_post_type_init() {
    // semua post type
    global $wpdb;
    $query_type = $wpdb->get_results( "
        SELECT *
        FROM " . $wpdb->prefix . "mf_posttypes
        ORDER BY name
        ASC
    " );

    // tampilkan post type
    foreach ( $query_type as $type ) {
        $args = array(
            'label'                 => __( $type->name, 'oelas' ),
            'supports'              => array( 'title', 'editor', 'excerpt', 'thumbnail', 'revisions', 'page-attributes' ),
            'taxonomies'            => array( strtolower($type->name).'_brand', 'post_tag' ),
            'hierarchical'          => true,
            'public'                => true,
            'show_ui'               => true,
            'show_in_menu'          => true,
            'show_in_admin_bar'     => true,
            'show_in_nav_menus'     => true,
            'can_export'            => true,
            'has_archive'           => true,
            'rewrite'               => array( 'slug' => strtolower($type->name) ),
        );

        register_post_type( $type->type, $args );

        add_rewrite_rule( 
            '^'.strtolower($type->name).'/([^/]+)/?$',
            'index.php?post_type='.$type->type.'&name=$matches[1]',
            'top'
        );
    }
}

add_action( 'init', 'rewrite_post_type_init' );

function rewrite_post_type_flatten_hierarchies( $post_link, $post ) {
    // semua post type
    global $wpdb;
    $query_type = $wpdb->get_results( "
        SELECT *
        FROM " . $wpdb->prefix . "mf_posttypes
        ORDER BY name
        ASC
    " );

    // tampilkan post type
    foreach ( $query_type as $type ) {
        if ( strtolower($type->name) != $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', 'rewrite_post_type_flatten_hierarchies', 10, 2 );
/***************** akhir */

你使用的是什么parmalink结构?用下面的代码修复。,