問題描述
我正在使用自定義固定鏈接”/%category%/%postname%/” 。每當標題包含引號或撇號時,它們都會顯示在 URL 中。
有人可以告訴我如何防止他們出現在 s 子裏?
我正在運行 WordPress 3.0.4 。
最佳解決方案
在 WordPress 中,”—“ 和 「 – 」 成為 em-dashes( – —),”–“ 成為 en-dash(-#8212;) 。 sanitize_title_with_dashes()功能沒有抓住這些。
該功能使用數據庫副本,但顯示給用户的標題總是通過一個紋理化函數。所以如果我們在數據庫中替換 en /em 破折號,最終的結果將是一樣的,並避免這些不良的 URL 的情況,標題是 re-texturized 。
add_action( 'title_save_pre', 'do_replace_dashes' );
function do_replace_dashes($string_to_clean) {
# The html entities (– and —) don』t actually work but I include them for kicks and giggles.
$string_to_clean = str_replace( array('—', '—', '–', '–', '‚', '„', '「', '」', '』', '『', '…'), array(' -- ',' -- ', '--','--', ',', ',,', '"', '"', "'", "'", '...'), $string_to_clean );
return $string_to_clean;
}
次佳解決方案
我看到有一些插件可以解決這個問題。檢查 Clean URL 例如:
This simple WordPress plugin is used when generating article slug (= article name used in URL). It removes all characters other than letters a-z, numbers and hyphens (-). The plugin runs as the last one in the whole url-generating process, so first all standard replacements of diacritics and accents are performed, and then, if still some strange characters are present they are cut out.
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。