那朵莲 發表於 2023-10-7 00:00:00

wordpress在postname中支持大写字母的方法

<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
一般而言,WordPress本身会对英语用户的体验进行优化,例如默认模板的分隔符永远都是“|”,因为英文单词以空格空开,要区分两个单词不可能用“-”,因此“|”是最好的选择。同样,为了让URL更符合浏览器解析和用户的识别,wordpress默认会将标题中的英文大写字母lower到小写。</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
然而对于中文网站来说,这或许不是一件很好的是,如果要在URL中使用中文,那么使用大写也是常有的,这里提供一种取消wordpress自动降级字母大写的方法。<br>
 </p>
<div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<div style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span>
</div>
<p>
代码如下:</p>
</div>
<div id="phpcode5" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
<br>
remove_filter( 'sanitize_title', 'sanitize_title_with_dashes' );<br>
add_filter( 'sanitize_title', 'use_capital_letter_in_slug' );<br>
function use_capital_letter_in_slug($title) {<br>
$title = strip_tags($title);<br>
// Preserve escaped octets.<br>
$title = preg_replace('|%()|', '---$1---', $title);<br>
// Remove percent signs that are not part of an octet.<br>
$title = str_replace('%', '', $title);<br>
// Restore octets.<br>
$title = preg_replace('|---()---|', '%$1', $title);&lt;/p&gt; &lt;p&gt; $title = remove_accents($title);<br>
if (seems_utf8($title)) {<br>
//if (function_exists('mb_strtolower')) {<br>
// $title = mb_strtolower($title, 'UTF-8');<br>
//}<br>
$title = utf8_uri_encode($title, 200);<br>
}&lt;/p&gt; &lt;p&gt; //$title = strtolower($title);<br>
$title = preg_replace('/&amp;.+?;/', '', $title); // kill entities<br>
$title = str_replace('.', '-', $title);<br>
// Keep upper-case chars too!<br>
$title = preg_replace('/[^%a-zA-Z0-9 _-]/', '', $title);<br>
$title = preg_replace('/\s+/', '-', $title);<br>
$title = preg_replace('|-+|', '-', $title);<br>
$title = trim($title, '-');&lt;/p&gt; &lt;p&gt; return $title;<br>
}</div>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
 </p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
将上面的代码拷贝到你的主题文件functions.php中,它即可取消wordpress对postname的格式化,同样,它也对分类、标签等的别名起作用。</p>
頁: [1]
查看完整版本: wordpress在postname中支持大写字母的方法