2014-10-30 50 views
0

我有默認的鏈接到我的文章'/ video/translate/continuous_present'(continuous_present是一個帖子名)',我想讓它看起來更像這樣'/ exercise/continuous_present',但以前的URL也應該可用。自定義帖子類型的WordPress替代URL

我試圖創建一個重寫規則,它的工作原理,但重定向我舊的鏈接

add_action('init', function() { 
    add_permastruct('single-exercise', '^exercise/%postname%', array('with_front' => false)); 
    flush_rewrite_rules(); 
} 


$wp_rewrite變量包含這樣的值

WP_Rewrite Object 
(
[permalink_structure] => /video/%postname% 
[front] => /video/ 
... 

下面是我註冊我的自定義文章類型:

register_post_type('translate', 
    array(
     'labels' => array(
      'name' => 'Упражнения', 
      'singular_name' => 'Упражнение' 
     ), 
     'public' => true, 
     'has_archive' => true, 
     'rewrite' => array('slug' => 'translate'), 
     'hierarchical' => true, 
     'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'page-attributes', 'comments', 'revisions', 'post-formats'), 
     'menu_position' => 6, 
     'taxonomies' => array('translates_category', 'category'), 
    ) 
); 

register_taxonomy('translates_category',array('translate'), array(
'labels' => array(
    'name' => 'Рубрики упражнений', 
    'singular_name' => __('Category', 'engl') 
), 
'hierarchical' => true, 
'rewrite' => array('slug' => 'translates-category'), 
)); 

有什麼建議嗎?

PS: nginx的(無阿帕奇)

回答

0

後google搜索,並通過WP的核心文件挖個小時,我發現了一個解決方案!所以這裏是我得到的

add_action('init', function() { 
    add_rewrite_tag('%single-exercise-postname%', '([^/]+)', 'post_type=translate&name='); 
    add_permastruct('single-exercise', '^exercise/%single-exercise-postname%', array('with_front' => false)); 
    flush_rewrite_rules(); // fire it only once 
}); 

add_filter('post_type_link', function($post_link, $id = 0, $leavename) { 
    global $wp_rewrite; 
    $post = &get_post($id); 
    if (is_wp_error($post)) 
     return $post; 

    if ($post->post_type == 'translate') { 
     $newlink = $wp_rewrite->get_extra_permastruct('single-exercise'); 
     $newlink = str_replace('^', '', $newlink); 
     $newlink = str_replace("%single-exercise-postname%", $post->post_name, $newlink); 
     $newlink = home_url(user_trailingslashit($newlink)); 
     return $newlink; 
    } 

    return $post_link; 
}, 1, 3); 
相關問題