2016-12-23 18 views
0

我使用名爲Dreamland的wordpress主題,它已經定義了一些自定義帖子類型和分類法。我想要改變URL中省略slu per的永久鏈接結構。幾乎我找到的每個解決方案都基於register_post_type()函數上的重寫參數。我試圖直接改變它在哪裏的自定義後類型註冊地或register_post_type_args過濾器是這樣的:爲自定義帖子類型重寫url

function custom_post_type_args($args, $post_type) { 
    if ($post_type == "bunch_property") { 
     $args['rewrite'] = array(
      'slug' => '' 
     ); 
    } 

    return $args; 
} 
add_filter('register_post_type_args', 'custom_post_type_args', 999, 2); 

它總是與404錯誤結束。重要的信息是,只有允許的slu is是「財產」,儘管這個帖子類型的真正slu is是「bunch_property」。我的同類問題叫做「property_category」,但我通過this solution解決了它。是否還有類似的郵政類型而不是分類?或者除了在.htaccess上重寫規則之外的任何其他解決方案?

+0

這工作正常,如果永久鏈接結構是%postname%,但我想我自己的永久鏈接方案: /%property_category%/%postname% 不幸的是,在這種情況下不起作用。 –

回答

1

嘗試此解決方案Remove Custom Post Type Slug from Permalinks

功能gp_remove_cpt_slug($ POST_LINK,$後,$ leavename){

if ('bunch_property' != $post->post_type || 'publish' != $post->post_status) { 
     return $post_link; 
    } 

    $post_link = str_replace('/' . $post->post_type . '/', '/', $post_link); 

    return $post_link; 
} 
add_filter('post_type_link', 'gp_remove_cpt_slug', 10, 3); 


function gp_parse_request_trick($query) { 

// Only noop the main query 
if (! $query->is_main_query()) 
    return; 

// Only noop our very specific rewrite rule match 
if (2 != count($query->query) || ! isset($query->query['page'])) { 
    return; 
} 

// 'name' will be set if post permalinks are just post_name, otherwise the page rule will match 
if (! empty($query->query['name'])) { 
    $query->set('post_type', array('post', 'page', 'bunch_property')); 
} 
} 
add_action('pre_get_posts', 'gp_parse_request_trick'); 

添加自定義分類後輸入url

add_filter('generate_rewrite_rules', 'taxonomy_slug_rewrite'); 

function taxonomy_slug_rewrite($wp_rewrite) { 
    $rules = array(); 
    // change "^property/([^/]+)/([^/]+)/?" to "^([^/]+)/([^/]+)/?" to test without "property" in url 
    $rules["^property/([^/]+)/([^/]+)/?"] = 'index.php?post_type=bunch_property&property_category=$matches[1]&resource=$matches[2]'; 
    // merge with global rules 
    $wp_rewrite->rules = $rules + $wp_rewrite->rules; 
} 
+0

@PavelNěmec你的網址是/ property/slug還是/ bunch_property/slug? –

+0

Now/property/slug。 其實您的解決方案工作正常,如果永久鏈接結構設置爲%postname%,但我希望alson包含分類在永久鏈接:/%property_category%/%postname%。不幸的是,在這種情況下不會工作。結果是單個帖子頁面上的空白頁面? –

+0

@PavelNěmec我在一個項目中遇到了同樣的問題,我可以通過添加我添加到解決方案中的代碼來修復它。但在我的情況下,我離開職位類型slu((在我的情況下資源,爲您的財產)。我建議離開它,否則你測試沒有,你得到什麼預期 –

0

最後我解決這個問題感謝Sofiane Achouba。除了由add_rewrite_rule解決的最後一部分。這不是最優的,因爲我要叫環路去throught所有帖子和應用add_rewrite_rule()函數爲他們每個人:

function my_custom_rewrite() { 
    $args = array("post_type" => "bunch_property","posts_per_page" => -1); 
    $posts = get_posts($args); 
     foreach($posts as $post){ 
     $cat = get_the_terms($post,"property_category"); 
     $cat_slug = $cat[0]->slug; 
     $post_slug = $post->post_name; 
     add_rewrite_rule('^'.$cat_slug.'/'.$post_slug.'?/','index.php/property/'.$cat_slug.'/'.$post_slug.'/', 'top'); 
     } 
} 
add_action('init', 'my_custom_rewrite'); 

我試圖只去throught但名作它不工作。

很奇怪,但這個工程:

add_rewrite_rule('^xxx/(.*?)/', 'index.php/property/xxx/yyy/','top'); // where xxx is post name and yyy category slug 

但這doen't:

add_rewrite_rule('^xxx/(.*?)/', 'index.php/property/xxx/$matches/','top'); 

害得我上述的解決。再次重複它不是最佳的。

相關問題