你至少有三种方法可以做到这一点。
Option 1
最简单的方法是将permalink结构更改为“Post Name”,然后使用所需的slug创建登录页。所以,你对约克的评价应该是“约克的教练”。
Option 2
如果必须修改url,可以使用add\\u rewrite\\u规则来完成。将其放入函数中。php(或插件文件)
<?php
// Create taxonomy endpoint
function taxonomy_endpoint()
{
// Add the rewrite rule.
add_rewrite_rule(\'^(trainers-in-[a-zA-Z-]{2,})/?\', \'town/$matches[2]\', \'top\');
}
add_action( \'init\', \'taxonomy_endpoint\' );
上面重写的url(以“town”开头)只是一个示例。您必须将其更改为您希望用户指向的任何url$匹配项[2]将用用户提供的位置填写。例如:
示例。约克的com/trainers将更改为示例。com/城镇/约克Option 3
如果需要执行更复杂的操作,例如根据url更改$wp\\u查询,可以通过重写标记和pre\\u get\\u posts操作来完成。
// Create taxonomy endpoint
function taxonomy_endpoint()
{
// Add variables to populate from the url
add_rewrite_tag(\'%my_taxonomy%\', \'([a-zA-Z-]{2,})\' );
// Add the rewrite rule. This sets the "my_taxonomy" variables
add_rewrite_rule(\'^(trainers-in-[a-zA-Z-]{2,})/?\', \'index.php?my_taxonomy=$matches[2]\', \'top\');
}
// Hook into init, so this is done early
add_action( \'init\', \'taxonomy_endpoint\' );
// Checks if our "my_taxonomy" variable is set, and modifies the wp_query if it is
function taxonomy_redirect()
{
global $wp_query, $wpdb;
// If not the main query, don\'t change anything.
if ( !$query->is_main_query() )
return;
// Get our variables
$taxonomy = $wp_query->get(\'my_taxonomy\');
// If my_taxonomy is NOT set, then continue on without changing anything
if (!$taxonomy)
return;
// Put your code to do something to the post here...
// For example... This sets the page_id to 10.
$query->set(\'page_id\',10);
// Realistically, you would fill this part with get_post() or
// wpdb->get_results or some other logic to select data
// to change the query
}
// Hook into pre_get_posts, so this runs before the post data is retrieved
add_action( \'pre_get_posts\', \'taxonomy_redirect\' );
祝你好运!