2016-12-27 69 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); 
    } 
} 
相关问题