2016-12-27 73 views
0

我正在改變一個自定義帖子類型的固定鏈接,以在發佈ID之前包含分類。我有能力在URL中顯示分類,但是當我進入頁面時,我得到一個404錯誤。它看起來像固定鏈接的結構是正確的,但是帖子的位置沒有與數據庫位置同步。WordPress的固定鏈接生成404頁面

任何幫助表示感謝,並提前致謝!

夫婦的注意事項:

  • 我的.htaccess文件,對國防部重寫。
  • 我已經添加%的稅收%的永久改寫爲CPT
  • 我有存檔打開以CPT

代碼

function change_permalink($link, $post) { 
    if (‘custom-post-type-name’ == get_post_type($post)) { 
     // Get post 
     $post = get_post($post_id); 
     if (!$post) return $permalink; 
     // Get taxonomy terms 
     $terms = wp_get_object_terms($post->ID, ’taxonomy-name’); 
     if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $taxonomy_slug = $terms[0]->slug; 

     else $taxonomy_slug = 'no-taxonomy-listed’; 

     return str_replace('%tax%', $taxonomy_slug, $link); 
    } 
    return $link; 
    } 
    add_filter('post_type_link', ‘change_permalink’, 10, 2); 

回答

0

想通了。需要wp_rewrite:

add_action('wp_loaded', 'urban_permastructure'); 
function urban_permastructure($post) { 
    if ('custom-post-type-name' == get_post_type($post)) { 
    global $wp_rewrite; 
    $structure = '/cat/%tax%'; 
    $wp_rewrite->add_rewrite_tag("%tax%", '([^/]+)', "tax-type="); 
    $wp_rewrite->add_permastruct('tax-type', $structure, false); 
    } 
}