WordPress - 需要自定义帖子的一致URL

时间:2016-05-10 01:21:43

标签: php wordpress url url-rewriting custom-post-type

我使用以下代码创建了自定义帖子:

// Our custom post type function
    function create_posttype() {

    register_post_type( 'tours',
    // CPT Options
        array(
            'labels' => array(
                'name' => __( 'Tours' ),
                'singular_name' => __( 'Tour' )
            ),
            'public' => true,
            'has_archive' => true,
            'rewrite' => array('slug' => 'tours'),
            'hierarchical' => true 
        )
    );
}
// Hooking up our function to theme setup
add_action( 'init', 'create_posttype' );

function my_taxonomies_tours() {
  $labels = array(
    'name'              => _x( 'Tour Categories', 'taxonomy general name' ),
    'singular_name'     => _x( 'Tour Category', 'taxonomy singular name' ),
    'search_items'      => __( 'Search Tour Categories' ),
    'all_items'         => __( 'All Tour Categories' ),
    'parent_item'       => __( 'Parent Tour Category' ),
    'parent_item_colon' => __( 'Parent Tour Category:' ),
    'edit_item'         => __( 'Edit Tour Category' ), 
    'update_item'       => __( 'Update Tour Category' ),
    'add_new_item'      => __( 'Add New Tour Category' ),
    'new_item_name'     => __( 'New Tour Category' ),
    'menu_name'         => __( 'Tour Categories' ),
  );
  $args = array(
    'labels' => $labels,
    'hierarchical' => true,
    //'rewrite' => array('slug' => 'tours')
  );
  //register_taxonomy( 'tour_category', 'tours', $args );
  register_taxonomy( 'tour_category', 'tours', $args );
}

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

在我的永久链接设置中,我已设置:

http://mydomainname.com.au/%category%/%postname%/

我想像这样显示网址:

按类别显示帖子

http://mydomainname.com.au/tours/gbr/
http://mydomainname.com.au/tours/whitehaven-tours/
http://mydomainname.com.au/tours/day-tours/

展示帖子

http://mydomainname.com.au/tours/gbr/name-of-post
http://mydomainname.com.au/tours/whitehaven-tours/name-of-post
http://mydomainname.com.au/tours/day-tours/name-of-post

但我目前正在获取以下网址:

按类别显示帖子

http://mydomainname.com.au/tour_category/gbr/
http://mydomainname.com.au/tour_category/whitehaven-tours/
http://mydomainname.com.au/tour_category/day-tours/

展示帖子

http://mydomainname.com.au/tours/name-of-post
http://mydomainname.com.au/tours/name-of-post
http://mydomainname.com.au/tours/name-of-post

我对URL重写不够熟悉以进行必要的更改。我读了很多文章,但没有得到快速。非常感谢任何正确方向的帮助。

0 个答案:

没有答案