简体   繁体   English

如何在Wordpress中将PHP字符串转换为子弹

[英]How do I turn PHP string into slug in Wordpress

I have a page in wordpress I am using with a slug called Bad-Debt-Recovery-in/ . 我在wordpress中有一个页面,正在使用一个名为Bad-Debt-Recovery-in/ I am using a custom php query on that page with strings in the URL's like this 我正在该页面上使用自定义php查询,URL中的字符串是这样的

Bad-Debt-Recovery-in/?zipcode=55555&location=Chambers%20County+AL

How can I make this url into a slug like this 我怎样才能使这个URL变成这样的

Bad-Debt-Recovery-in/55555/Chambers-County/AL/

as a rewrite? 作为重写? Any help would be appreciated! 任何帮助,将不胜感激!

UPDATE: This code is actually what I am using. 更新:这段代码实际上是我正在使用的。 I also made it simpler and created a third variable named "state". 我还简化了操作,并创建了第三个名为“ state”的变量。 One rewrite is for City and one is for County page: 一种是针对城市的重写,另一种是针对县的重写:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^Bad-Debt-Recovery-And-Collection-Agencies-Services-In\/([^\/]+)\/([^\/]+)\/([^\/]+)\/? Bad-Debt-Recovery-And-Collection-Agencies-Services-In/?zipcode=$1&city=$2&state=$3 [QSA,L,NC]
RewriteRule ^Bad-Debt-Recovery-And-Collection-Agency-Services-In\/([^\/]+)\/([^\/]+)\/([^\/]+)\/? Bad-Debt-Recovery-And-Collection-Agency-Services-In/?countyid=$1&county=$2&state=$3 [QSA,L,NC]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php

</IfModule>

# END WordPress

You can use the WP function sanitize_title 您可以使用WP 函数sanitize_title

Combine this with a php loop ie 结合这个与PHP循环即

$url = $_SERVER['REQUEST_URI'];
foreach($_GET as $g){
  $url .= '/'.$g;
}
$url = sanitize_title($url);

What you are asking for, and seeking to accomplish is called: Converting a request path into a query string. 您要求和寻求完成的工作称为:将请求路径转换为查询字符串。

Use .htaccess RewriteRule directive to modify the incoming request 使用.htaccess RewriteRule指令修改传入请求

RewriteRule ^Bad-Debt-Recovery-in\/([^\/]+)\/([^\/]+)\/([^\/]+)\/? Bad-Debt-Recovery-in/?a=$1&b=$2&c=$3 [QSA,L,NC]

Each ([^\\/]+) captures path elements into a variables $1,$2,$3... 每个([^\\/]+)将路径元素捕获到变量$1,$2,$3...

The ? ? at the end simply denotes that the last / is optional or else the last match could fail 最后仅表示last /是可选的,否则最后的匹配可能失败

the \\ simply escape the '/' for literal interpretation \\只是将'/'转义为文字解释

Add as many ([^\\/]+) as needed and capture them in the query 根据需要添加尽可能多的([^\\/]+)并将其捕获到查询中

zipcode=$1&location=$2+$3

The modifiers at the end [QSA,L,NC] are called flags [QSA,L,NC]结尾的修饰符称为标志

  • QSA appends the query string to the end of the rewrite if any exist QSA将查询字符串附加到重写的末尾(如果存在)
  • L simply says this is the last rewrite L只是说这是最后一次重写
  • NC means not case sensitive NC表示不区分大小写

This is your final solution: 这是您的最终解决方案:

RewriteRule ^Bad-Debt-Recovery-in\/([^\/]+)\/([^\/]+)\/([^\/]+)\/? Bad-Debt-Recovery-in/?zipcode=$1&location=$2+$3 [QSA,L,NC]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM