讓人怒吼的WordPress 分類 404

今天Sophia特別告訴我工作達人下方的連結都點不進去,這奇怪的問題ㄚ琪一直搞不懂是server升級後的問題,還是Wordpress升級後的問題造成。

ㄚ琪就把問題假設是固定網址的問題,發現好像不是。

再把問題假設成是Apache 的mod_rewrite沒有開,去檢查也有開?

繼續找了一個早上,發現How To Fix the 3.1 Custom Permalinks Bug這一篇提到,這是3.1版的問題?

好奇怪以前怎沒有注意到這個問題?好吧,先把它當死馬醫吧。

ㄚ琪在自製佈景主題裡的functions.php中放入了文中建議的程式碼:

// repairs the custom permalink bug with WordPress 3.1+
// works with or w/out custom permalinks enabled

add_action('init', 'wcs_hotfix_31_redirect_canonical', -1);

remove_filter('template_redirect', 'redirect_canonical');

function wcs_hotfix_31_redirect_canonical()
{
    // hotfix version: 0.97
    // developed by Luke America with valuable assistance by Jonas Nordström

    // source code release 2011-03-23
    // updated 2011-04-05 (added fixes to pagination for searches, categories, & tags)
    // updated 2011-04-08 (added support for multisites that use subdirectories)
    // updated 2011-04-09 (added hotfix bypass to retain XML-RPC Support)
    // updated 2011-04-11 (added fixes for RSS feeds for categories & tags)

    global $wp_version;

    // does NOT assume bug will be fixed by next version release
    if ((!is_admin()) && ($wp_version >= 3.1))
    {
        // extract current URI
        $uri = untrailingslashit($_SERVER['REQUEST_URI']);

        // bypass hotfix to retain XML-RPC Support
        $pos = strpos($uri, 'xmlrpc.php');
        if ($pos >= 1) {return;}

        // process hotfix for custom permalink CAT lookup
        $pos = strpos($uri, 'category/');
        if ($pos >= 1)
        {
            // prep fix for CAT rss feeds
            $feed = '';
            if (strpos($uri, 'feed'))
            {
                $feed = '&feed=rss2';
                $uri = substr($uri, 0, strlen($uri) - 5);
            }
            // continue CAT hotfix
            $pos = strrpos($uri, '/');
            $len = strlen($uri);
            $cat_slug = substr($uri, $pos + 1, $len - $pos - 1);
            $cat_id_object = get_category_by_slug($cat_slug);
            $cat_id = $cat_id_object->term_id;
            $url = site_url('?cat=' . $cat_id . $feed);
            header("Location: $url");
            exit;
        }

        // process hotfix for custom permalink TAG lookup
        $pos = strpos($uri, 'tag/');
        if ($pos >= 1)
        {
            // prep fix for TAG rss feeds
            $feed = '';
            if (strpos($uri, 'feed'))
            {
                $feed = '&feed=rss2';
                $uri = substr($uri, 0, strlen($uri) - 5);
            }
            // continue TAG hotfix
            $pos = strrpos($uri, '/');
            $len = strlen($uri);
            $tag_slug = substr($uri, $pos + 1, $len - $pos - 1);
            $url = site_url('?tag=' . $tag_slug . $feed);
            header("Location: $url");
            exit;
        }

        if (empty($_SERVER['QUERY_STRING']))
        {
             // handle true 404's, normal processing, etc
            redirect_canonical();
        }
        else
        {
            // fix pagination for categories, tags, and searches
            $page_query = wcs_hotfix_31_get_page($uri);
            if ($page_query != '')
            {
                $url = site_url() . $_SERVER['SCRIPT_NAME'] . '?' . $_SERVER['QUERY_STRING'];
                $url .= '&' . $page_query;
                header("Location: $url");
                exit;
            }

            // handle default queries (not needed)
            //$_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'] . '?' . $_SERVER['QUERY_STRING'];
        }
    }

    /**********************************************************************
     Copyright © 2011 Gizmo Digital Fusion (http://wpCodeSnippets.info)
     you can redistribute and/or modify this code under the terms of the
     GNU GPL v2: http://www.gnu.org/licenses/gpl-2.0.html
    **********************************************************************/
}
function wcs_hotfix_31_get_page($uri)
{
    // init
    $page = '';
    $pos = strpos($uri, 'page/');

    // convert to page query
    if ($pos >= 1)
    {
        $page = substr($uri, $pos + 5);
        $page = 'paged=' . intval($page);
    }

    // exit
    return $page;
}

OK!測試成功,收工,等等,網址那裡怎會變成非固定網址啊?還是覺得有點怪怪的,希望下一版的Wordpress有解。

感謝你看到這裡,很快就可以離開了,但最好的獎勵行動就是按一下幫我分享或留言,感恩喔~

點我分享到Facebook

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *