WordPress的URL重写自定义帖子

时间:2019-04-04 18:49:43

标签: php wordpress url-rewriting

嗨,我有一个自定义帖子名称“ trips”,其中包含网址格式 www.domain.com/trips/tripname /

我想从网址中删除帖子类型的基本名称“ trips”,并且还想要在网址中添加文字符号名称(国家名称),例如这样 www.domain.com/countryname/tripname /

$labels5 = array(
        'name'               => _x( 'Trips', 'post type general name', 'your-plugin-textdomain' ),
        'singular_name'      => _x( 'Trip', 'post type singular name', 'your-plugin-textdomain' ),
        'menu_name'          => _x( 'Trips', 'admin menu', 'your-plugin-textdomain' ),
        'name_admin_bar'     => _x( 'Trips', 'add new on admin bar', 'your-plugin-textdomain' ),
        'add_new'            => _x( 'Add New', 'client', 'your-plugin-textdomain' ),
        'add_new_item'       => __( 'Add New item', 'your-plugin-textdomain' ),
        'new_item'           => __( 'New item', 'your-plugin-textdomain' ),
        'edit_item'          => __( 'Edit item', 'your-plugin-textdomain' ),
        'view_item'          => __( 'View item', 'your-plugin-textdomain' ),
        'all_items'          => __( 'All', 'your-plugin-textdomain' ),
        'search_items'       => __( 'Search', 'your-plugin-textdomain' ),
        'parent_item_colon'  => __( 'Parent', 'your-plugin-textdomain' ),
        'not_found'          => __( 'No item found.', 'your-plugin-textdomain' ),
        'not_found_in_trash' => __( 'No item found in Trash.', 'your-plugin-textdomain' )
    );

    $args5 = array(
        'labels'             => $labels5,
        'description'        => __( 'Description.', 'your-plugin-textdomain' ),
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array('slug' => '/'), 
        'capability_type'    => 'post',
        'has_archive'        => false,
        'hierarchical'       => false,
        'menu_position'      => null,
        'taxonomies'          => array('destination','kontinent','triptype','tripform','travel-time'),
        'supports'           =>  array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
        'menu_icon'         => 'dashicons-businessman',
    );

    register_post_type('trips', $args5);


add_filter( 'post_type_link','custom_post_type_link',10,3);
function custom_post_type_link($permalink, $post, $leavename) {
    if (!gettype($post) == 'post') {
        return $permalink;
    }
    $tdest = '';
    $trip_destination = get_the_terms($post->ID, 'destination');
    if($trip_destination){
        $tdest = $trip_destination[0]->slug;
    }
    switch ($post->post_type) {
        case 'trips':
            if(!empty($tdest)){
                $permalink = get_home_url() . '/'.$tdest.'/' . $post->post_name . '/';
            } 

            break;
    }

    return $permalink;
}

任何帮助将不胜感激

0 个答案:

没有答案