你想要的WP_Rewrite::using_permalinks
.
<?php
add_action(\'init\', \'wpse142273_add_rules\');
function wpse142273_add_rules()
{
global $wp_rewrite;
if ($wp_rewrite->using_permalinks()) {
// Plan A
} else {
// Plan B
}
}
Here\'s a larger example.
<?php
// inside a class somewhere
public static function url($area, $additional=null)
{
global $wp_rewrite;
if ($wp_rewrite->using_permalinks()) {
$path = "/account/{$area}";
if ($additional) {
$path .= \'/\' . $additional;
}
if (\'/\' === $wp_rewrite->permalink_structure[count($wp_rewrite->permalink_structure) - 1]) {
$path = trailingslashit($path);
}
} else {
$q = array(
static::ACCOUNT_VAR => $area,
);
if ($additional) {
$q[static::ADDITIONAL_VAR] = $additional;
}
$path = \'?\'.http_build_query($q);
}
return apply_filters(\'frontend_accounts_url\', home_url($path), $area, $additional);
}
记住,盲目地打电话是可以的
add_rewrite_rule
不使用永久链接时。无论如何,您都需要添加自定义查询变量(通过筛选
query_vars
) 因此,您的插件/主题在不重写的情况下仍然可以正常工作。