什么是重写标记,什么是查询变量add_action( \'init\', \'ws361150_debug_test_pt\' );
function ws361150_debug_test_pt() {
// please comment/uncomment the debug output needed
// export an array list of all post types list
var_dump( get_post_types() );
// debug view one post type object by the following method
$post_types = get_post_types( [], \'objects\' );
var_dump($post_types[\'post\']); // put in the post type slug
}
// object(WP_Post_Type)[963]
// public \'name\' => string \'post\' (length=4) // post type slug
// ...
// public \'publicly_queryable\' => boolean true
// public \'query_var\' => boolean false
// public \'rewrite\' => boolean false // for post it is off, the settings is similar to taxonomy, if slug is set the rewrite is /%specified_slug%/, if true, it is by default /%post_slug%/, so that by default \'post\' is http://example.com/post-name/ instead of http://example.com/post_slug/post-name/
有许多方法需要完成,这里显示了两个方法,一个是重写,另一个是手动修改查询。我已经用示例帖子类型测试了这两种方法,并通过将它们应用到主题函数中完美地工作。php请不要同时放置方法1和方法2,因为只有一种方法建议用于成功的测试。在方法1中,add_permastruct 和add_rewrite_rule 已使用。add\\u permastruct-除了settings > Permalinks 在WordPress中,如果重写标记已注册,如分类法和帖子类型,则可以使用它创建自定义URL结构,添加自定义重写规则-添加自定义重写正则表达式以匹配查询URL,在某些情况下,需要执行自定义规则来执行任务方法1:重写
// objective: http://domain.com/cityname/hospitalname
// post type: \'hospital\', assumed the rewrite tag is \'%hospital%\'
// taxonomy: \'cities\', assumed the rewrite tag is \'%cities%\' and it is associated with hospital, otherwise, it will not work
add_action( \'init\', \'ws365575_taxonomy_rewrite1\' );
function ws365575_taxonomy_rewrite1() {
// optional settings as the 3rd argument for add_permastruct
// default
$args = array(
// \'with_front\' => true,
// \'ep_mask\' => EP_NONE,
// \'paged\' => true,
// \'feed\' => true,
// \'forcomments\' => false,
// \'walk_dirs\' => true,
// \'endpoints\' => true,
);
// this will need to flush the permalink cache to be effective
// add to $wp_rewrite->extra_permastructs
add_permastruct( \'cities_with_hospitals\', "%cities%/%hospital%", $args );
}
// objective: http://domain.com/hospitalname
// post_type: \'hospital\', assumed the query vars is \'%hospital%\'
// the top option tells WP to match it in higher priority before it is being regarded as not found
add_action( \'init\', \'ws365575_taxonomy_rewrite2\' );
function ws365575_taxonomy_rewrite2() {
// the rule is copied from originally registered post types \'hopsital\' part and modify by following page post type\'s rewrite
add_rewrite_rule(\'(.?.+?)/page/?([0-9]{1,})/?$\', \'index.php?hospital=$matches[1]&paged=$matches[2]\', \'top\');
add_rewrite_rule(\'(.?.+?)?(:/([0-9]+))?/?$\', \'index.php?hospital=$matches[1]&page=$matches[2]\', \'top\');
// because the above will override the priority, to avoid page cannot be loaded and become 404, manually add once more
add_rewrite_rule(\'(.?.+?)/page/?([0-9]{1,})/?$\', \'index.php?pagename=$matches[1]&paged=$matches[2]\', \'top\');
add_rewrite_rule(\'(.?.+?)?(:/([0-9]+))?/?$\', \'index.php?pagename=$matches[1]&page=$matches[2]\', \'top\');
}
**设置上述代码后,需要刷新permalink缓存。请参考以下注释**要确认注册的标记是否正确,可以转储$wp\\u重写全局变量。
// the following is for optional for debug
add_action( \'init\', \'ws365575_confirm_wp_rewrite\' );
function ws365575_confirm_wp_rewrite() {
global $wp_rewrite;
// if xdebug is not installed, it is recommended to view in source
var_dump($wp_rewrite);
exit(); // break the code to read
}
方法2:如果访问链接,请立即手动修改查询(高级)http://domain.com/hospitalname
并使其等效于http://domain.com/hospital/hospitalname
缺少post type
信息,它需要一些逻辑来分析和工作。过滤器request 提供对任何查询的早期控制,并且适合执行此工作。虽然有效,但请注意,这取决于教育WP进行分析的程度。因此,必须对不同的情况进行大量测试和深入思考,例如similar name
, 猜错了,异常处理。
***它不会吹WP,最多返回404或错误页面。以下内容通过放置在theme的函数中,在自定义帖子类型上进行测试。php
add_filter( \'request\', \'ws361150_custom_request_query\' );
function ws361150_custom_request_query( $query ) {
if ( isset( $_SERVER[\'HTTP_HOST\'] ) ) {
// build the URL in the address bar
$requested_url = is_ssl() ? \'https://\' : \'http://\';
$requested_url .= $_SERVER[\'HTTP_HOST\'];
$requested_url .= $_SERVER[\'REQUEST_URI\'];
}
if( url_to_postid( $_SERVER[\'REQUEST_URI\']) > 0 ) {
// if something is found priorly, nothing to do, suppose something exists
return $query;
} else {
// must think of a logic and test thoroughly before production
// manual setup the post query by guess
// check if rewrite slugs match original settings (spelling...etc)
if( $search_ID = url_to_postid( \'hospital\' . $_SERVER[\'REQUEST_URI\']) > 0 ) {
// setup query based on source code, if match by test
// check if post type name match settings
$query[\'post_type\'] = \'hospital\';
$query[\'hospital\'] = $query[\'name\'];
// var_dump($query);
// exit();
return $query;
}
}
return $query; // untouched
}
它看起来像一个简单的代码,开销更小,它在很大程度上依赖于提供的测试逻辑。重置永久链接(刷新缓存)
提醒,每当更改重写段塞或规则时,都需要刷新缓存。最简单的方法是settings > permalinks 然后单击保存。如果不更新缓存,它可能会坚持旧的设置,并浪费时间进行测试。设置URL后需要进行其他工作,然后需要使用post_link
和post_type_link
钩子以更改的链接get_permalinks()
或get_post_permalink()
获取链接。然后链接将反映在前端或后期编辑页面上,因为WP不会自动执行此操作。