我已经为网站上的各种自定义帖子类型创建了自定义永久链接。他们都工作了。直到最近,我才添加了另一个,我无法在8-o下工作。“company”结构不起作用,并给出了404。。
有人能发现我思维中的一个缺陷,或者让我走上正确的轨道吗?我一定忘了什么很愚蠢的事;-)
哦,我还转到了永久链接设置页面来刷新旧的永久链接/永久链接缓存/。。。
以下是自定义结构的代码:
// rewrite rewrite
global $wp_rewrite;
$wp_rewrite->add_rewrite_tag("%merkname%", \'([^/]+)\', "merk=");
$wp_rewrite->add_permastruct(\'merk\', \'/merken/%merkname%\', false);
$wp_rewrite->add_rewrite_tag("%modelname%", \'([^/]+)\', "model=");
$wp_rewrite->add_permastruct(\'model\', \'/merken/%merkname%/%modelname%\', false);
$wp_rewrite->add_rewrite_tag("%carname%", \'([^/]+)\', "car=");
$wp_rewrite->add_permastruct(\'car\', \'/merken/%merkname%/%modelname%/%carname%\', false);
$wp_rewrite->add_rewrite_tag("%companyname%", \'([^/]+)\', "company=");
$wp_rewrite->add_permastruct(\'company\', \'/company/%companyname%\', false);
function kw_custom_permalink($permalink, $post, $leavename) {
if ( \'\' != $permalink && !in_array($post->post_status, array(\'draft\', \'pending\', \'auto-draft\')) ) {
$merkname=\'\';
$modelname=\'\';
$carname=\'\';
$companyname=\'\';
if ( strpos($permalink, \'%merkname%\') !== false ) {
$merkname = $post->post_name;
}
if ( strpos($permalink, \'%modelname%\') !== false ) {
$merkpost = get_post( get_post_meta( $post->ID, "merk", true ) );
$merkname = $merkpost->post_name;
$modelname = $post->post_name;
}
if ( strpos($permalink, \'%carname%\') !== false ) {
$merkpost = get_post( get_post_meta( $post->ID, "_car_merk", true ) );
$merkname = $merkpost->post_name;
$modelpost = get_post( get_post_meta( $post->ID, "_car_model", true ) );
$modelname = $modelpost->post_name;
$carname = $post->post_name;
}
if ( strpos($permalink, \'%companyname%\') !== false ) {
$companyname = $post->post_name;
}
$permalink = str_replace(
array(
$leavename? \'\' : \'%postname%\',
\'%post_id%\',
\'%merkname%\',
\'%modelname%\',
\'%carname%\',
\'%companyname%\',
$leavename? \'\' : \'%pagename%\',
),
array(
$post->post_name,
$post->ID,
$merkname,
$modelname,
$carname,
$companyname,
$post->post_name,
),
$permalink
);
}
return $permalink;
}
add_filter(\'post_type_link\', \'kw_custom_permalink\', 10, 3);
SO网友:Tom J Nowell
您当前正在将重写规则置于全局范围内,这意味着它们会在加载文件后立即执行,这可能太早了。而是尝试将它们添加到init
钩子,或重写它们以使用generate_rewrite_rules
改为过滤,例如:
add_action(\'generate_rewrite_rules\', \'themes_dir_add_rewrites\');
function themes_dir_add_rewrites() {
$theme_name = next(explode(\'/themes/\', get_stylesheet_directory()));
global $wp_rewrite;
$new_non_wp_rules = array(
\'css/(.*)\' => \'wp-content/themes/\'. $theme_name . \'/css/$1\',
\'js/(.*)\' => \'wp-content/themes/\'. $theme_name . \'/js/$1\',
\'images/wordpress-urls-rewrite/(.*)\' => \'wp-content/themes/\'. $theme_name . \'/images/wordpress-urls-rewrite/$1\',
);
$wp_rewrite->non_wp_rules += $new_non_wp_rules;
}
Taken from Hongkiat
或
function josfaber_add_rules() {
global $wp_rewrite;
$wp_rewrite->add_rewrite_tag("%merkname%", \'([^/]+)\', "merk=");
$wp_rewrite->add_permastruct(\'merk\', \'/merken/%merkname%\', false);
$wp_rewrite->add_rewrite_tag("%modelname%", \'([^/]+)\', "model=");
$wp_rewrite->add_permastruct(\'model\', \'/merken/%merkname%/%modelname%\', false);
$wp_rewrite->add_rewrite_tag("%carname%", \'([^/]+)\', "car=");
$wp_rewrite->add_permastruct(\'car\', \'/merken/%merkname%/%modelname%/%carname%\', false);
$wp_rewrite->add_rewrite_tag("%companyname%", \'([^/]+)\', "company=");
$wp_rewrite->add_permastruct(\'company\', \'/company/%companyname%\', false);
}
add_action( \'init\', \'josfaber_add_rules\' );