在我的单曲里。php我从一个$\\u GET[]值中获取一个数据,我使用如下
domain.com/example-slug/?holder=value
我想要的是domain.com/example-slug/value 或domain.com/example-slug/holder/value那么,我怎样才能做到这一点呢?
在我的单曲里。php我从一个$\\u GET[]值中获取一个数据,我使用如下
domain.com/example-slug/?holder=value
我想要的是domain.com/example-slug/value 或domain.com/example-slug/holder/value那么,我怎样才能做到这一点呢?
您不需要对重写规则执行任何操作。
您的示例(“domain.com/example slug/?holder=value”)是“普通”永久链接的完美示例。因此,您的出发点是将Permalinks(设置>Permalinks)设置为“普通”以外的值。这将需要“/?”组件,并以首选格式生成URL。
第二件事是捕获URL,对其进行解析并获取“值”(使用您的文字)。您可以使用下面的函数来完成此操作。将此函数放入函数中。php(或者一个插件,如果你喜欢的话)。然后在你想获取“值”的页面/帖子上调用它。该函数将以变量$myvalue的形式生成“value”。您可以扩展函数以执行更多操作,也可以在其他地方返回值以执行操作。
function getslug(){
// get the url
$myurl = home_url( $wp->request );
//echo "<p>The page url is ".$myurl." </p>"; //DEBUG
// Explode the url to get access to the taxonomy and terms
$pageterms = explode(\'/\', $myurl);
//echo "the exploded components of the URL <pre>";print_r($pageterms);echo "</pre>"; //DEBUG
// the value is always the last element in the url, so we\'ll count the number of elements
$mycount = count($pageterms);
//echo "<p>The element count is ".$mycount." </p>"; //DEBUG
// since array keys always start with zero, the key for the value will be the count minus 1 (to allow for the zero)
$myvalue = $pageterms[$mycount-1];
//echo "<p>My value is ".$myvalue." </p>"; //DEBUG
}
PS: 我在本例中留下了echo语句(用于调试);如果不需要/不想要,可以直接删除它们。您可以使用如下重写规则重写URL
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ domain.com/$1 [L,R=301,NC]
RewriteRule ^example-slug/value $ example-slug/?holder=$1 [L]
您可以将重写函数过滤器添加到“站点特定函数插件”或主题函数中。php
编辑功能后。php或激活插件,您需要flush WP\'s rewrite rules. 转到管理仪表板>设置>永久链接,然后单击保存(然后将应用新规则)
示例(未测试):
For posts:
<?php
/*
Plugin Name: my post rewrite plugin
*/
function my_post_rules($rules)
{
global $wp_rewrite;
$my_rule = array(
\'(.+?)/(.+?)$\' => \'index.php?category_name=$matches[1]&holder=$matches[2]\'
// or
\'example-slug/(.+?)$\' => \'index.php?category_name=example-slug&holder=$matches[1]\'
);
return array_merge($my_rule, $rules);
}
add_filter(\'post_rewrite_rules\', \'my_post_rules\');
For pages:
function my_page_rules($rules)
{
global $wp_rewrite;
$my_rule = array(
\'(.+?)/(.+?)$\' => \'index.php?page_name=$matches[1]&holder=$matches[2]\'
// or
\'example-slug/(.+?)$\' => \'index.php?page_name=\' . \'example-slug&holder=$matches[1]\'
);
return array_merge($my_rule, $rules);
}
add_filter(\'page_rewrite_rules\', \'my_page_rules\');
WP的使用get_query_var函数可能是不必要的,但在其他情况下使用“获取值”可能需要它。因此,在模板中使用get\\u query\\u var并通过“声明”查询var是一个好习惯query_var filter。我上传了一个名为page testtest的文件。php到我的模板文件夹。当我去example.com/testtest这对我来说似乎很奇怪,因为文档清楚地表明我应该毫不费力地去查阅那个文件。相反,我得到了“哎呀!找不到那个页面。”顺便说一下,永久链接设置为:https://example.com/sample-post/ 我使用下划线模板在有人勇敢地让我查阅文件之前https://developer.wordpress.org/themes/template-files-section/page-tem