2017-08-26 48 views
2

我有一個Custom Post Type稱爲書,並且鏈接是:mywebsite.com/book/mybookname如何在重寫URL以排除子彈後刪除舊的自定義帖子類型固定鏈接?

我想改變這一點,以便鏈接mywebsite.com/mybookname

我加入以下代碼更改的鏈接,它按預期工作:

function books_theme_remove_slug($post_link, $post, $leavename) { 

    if ('book' != $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', 'books_theme_remove_slug', 10, 3); 

function books_theme_parse_request($query) { 

    if (! $query->is_main_query() || 2 != count($query->query) || ! isset($query->query['page'])) { 
     return; 
    } 

    if (! empty($query->query['name'])) { 
     $query->set('post_type', array('post', 'book', 'page')); 
    } 
} 
add_action('pre_get_posts', 'books_theme_parse_request'); 

的問題是,舊的鏈接(mywebsite.com/book/mybookname)仍正常工作。我想讓鏈接轉到404頁面而不會打破當前鏈接。

我嘗試以下,但它打破了一切:

function books_theme_parse_request($query) { 
    if(isset($query->query['post_type']) && $query->query['post_type'] == 'book'){ 
     global $wp_query; 
     $wp_query->set_404(); 
     status_header(404); 
     get_template_part(404); exit(); 
    } 

    if (! $query->is_main_query() || 2 != count($query->query) || ! isset($query->query['page'])) { 
     return; 
    } 

    if (! empty($query->query['name'])) { 
     $query->set('post_type', array('post', 'book', 'page')); 
    } 
} 
add_action('pre_get_posts', 'books_theme_parse_request'); 

如何刪除舊的網址是什麼?

+0

不知道這事,但你爲什麼要使用書本 - 西班牙語單詞>時,根據您的其他代碼你正在使用您的文章類型英文單詞LIBRO? – fjoe

+0

你把你試過的代碼放在哪裏?我想應該增加更多的條件,比如要知道這是網站的管理員或公共部分,頁面的類型等。順便說一句,它是如何打破一切的?錯誤代碼? – Kaddath

+0

fjoe:它是一個西班牙語網站,我在這裏使用了書籍,因爲我認爲如果說英語的人可以很容易地獲得幫助,我更容易獲得幫助,所以我編輯了它。 Kaddath,我試過的代碼是在books_theme_parse_request($ query)中。虐待編輯我的帖子,使其更清晰。它會讓它剎車,因爲在任何地方顯示的書都會變成404錯誤。我只想要與原始網址的書單是404 – Aschab

回答

2

這不應該是首先發生,所以你不應該試圖通過編程來修復它 - 相反,你應該在源代碼修復它。嘗試找出原因並加以解決。否則,你可以引入其他問題。

一些可能的解決方案,根據不同的病因:

  1. 刷新你重寫緩存
    WordPress的不寫入重定向到的.htaccess,它使用rewrite rules解析URL並找到一個匹配爲重定向。 這意味着如果你不刷新你的重寫規則,舊的鏈接仍然有效。 編號:SarahCoding's answer to 'Remove Old Permalinks?'

    如何做到這一點:
    重新保存永久鏈接將刷新重寫規則,但如果不工作也有Three Ways to Flush the Rewrite Cache in WordPress
     

  2. 清除緩存
    如果您安裝了緩存插件,則需要清除它們。一些安全插件也使用緩存,例如SECURI。它也可以緩存在瀏覽器中。

    如何做到這一點:
    How to Clear Your Cache in WordPress
     

  3. 刪除舊的WP固定鏈接
    當您更新蛞蝓,舊的永久鏈接仍保存在數據庫中。這可能會導致問題,如果你想使用你曾經用過的slu for例子。

    如何做到這一點:
    舊的固定鏈接存儲在表postmeta_wp_old_slugmeta_key。要清除所有的老蛞蝓的,在你的WP數據庫運行此查詢:
    DELETE FROM wp_postmeta WHERE meta_key = '_wp_old_slug';

    參考Mark Dave Tumanda's answer to 'Remove Old Permalinks?'
     

  4. 檢查重定向插件
    如果您正在使用的任何重定向插件,請檢查重定向規則,以防有任何與您的新網址衝突的情況。

0

基於您的評論你需要接着就是知道如果URL包含/書...請參閱下面的附加剪斷-它:

function books_theme_parse_request($query) { 
    global $wp; 
    $current_url = home_url($wp->request); 

    if (strpos($current_url, "/book" == false)) { return; } 

    if(isset($query->query['post_type']) && $query->query['post_type'] == 'book'){ 
     global $wp_query; 
     $wp_query->set_404(); 
     status_header(404); 
     get_template_part(404); exit(); 
    } 

    if (! $query->is_main_query() || 2 != count($query->query) || ! isset($query->query['page'])) { 
     return; 
    } 

    if (! empty($query->query['name'])) { 
     $query->set('post_type', array('post', 'book', 'page')); 
    } 
} 
add_action('pre_get_posts', 'books_theme_parse_request'); 

當人們提到reference

一關於管理員使用的警告

此過濾器也可用於影響管理屏幕查詢。請務必檢查您的修改是否影響您的帖子編輯屏幕。例如,只檢查is_main_query()和is_post_type_archive('movie')也會更改對edit.php?post_type = movie管理屏幕的查詢,除非您還檢查!is_admin()

相關問題