我有一些用于自定义帖子类型的自定义永久链接,如下所示:
/产品/类别/子类别/产品名称
我写了一个generate_rewrite_rules
用于与该自定义帖子类型相关的永久链接的函数。
然而,我通过WP的界面创建的任何常规页面都显示为404,而这些自定义永久链接仍然有效。我确信这是Apache的问题。不知道怎么了。
我有一个可编辑的。htaccess到位,里面有正常的东西。。。
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
相关服务器的apache conf文件:
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /path/to/folder>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
Order allow,deny
allow from all
</Directory>
在Ubuntu 14.04上编辑正在运行的WP 4.8.1
编辑2:刚刚注意到这是WP 404,不是apache 404。因此,这取决于我的重写规则,我注意到重写是寻找每个页面作为我的自定义分类法之一,如(index.php?product category=$matches[1]),这就是我得到404的原因。当然,这不应该发生。
如何修复它?我的功能有点像鸟巢。
function custom_rewrite_rules($wp_rewrite) {
$newRules = array();
$terms = get_terms( array(
\'taxonomy\' => \'product-category\',
\'hide_empty\' => false,
) );
foreach ($terms as $term) {
$post_query = new WP_Query( array(
\'post_type\' => \'custom_product\',
\'posts_per_page\' => -1,
\'tax_query\' => array(
array(
\'taxonomy\' => \'product-category\',
\'field\' => \'slug\',
\'terms\' => $term->slug,
\'include_children\' => false
),
)
));
if ( $post_query->have_posts() ) : while ( $post_query->have_posts() ) : $post_query->the_post();
if($term->parent){
$parent = get_term( $term->parent, \'product-category\');
$newRules[\'products/\' . $parent->name . \'/\' . $term->name . \'/\'. get_post_field( \'post_name\', get_the_ID() ) . \'$\'] = \'index.php?taxonomy=product-category&post_type=custom_product&product-category=\'.$parent->slug.\'&product-category=\'.$term->slug.\'&custom_product=\'. get_post_field( \'post_name\', get_the_ID() );
} else {
$newRules[\'products/\' . $term->name . \'/\'. get_post_field( \'post_name\', get_the_ID() ) . \'$\'] = \'index.php?taxonomy=product-category&post_type=custom_product&product-category=\'.$term->slug.\'&custom_product=\'.get_post_field( \'post_name\', get_the_ID() );
}
endwhile;
endif;
if($term->parent){
$parent = get_term( $term->parent, \'product-category\');
$newRules[\'products/\' . $parent->name . \'/\' . $term->name . \'$\'] = \'index.php?taxonomy=product-category&product-category=\'.$parent->slug.\'&product-category=\'.$term->slug;
} else {
$newRules[\'products/\' . $term->name . \'$\'] = \'index.php?taxonomy=product-category&product-category=\'.$term->slug;
}
}
$newRules[\'products/(.+)/(.+)/?$\'] = \'index.php?post_type=custom_product&custom_product=$matches[2]\';
$newRules[\'products$\'] = \'index.php?post-type=page&pagename=product-categories\';
$wp_rewrite->rules = $newRules + $wp_rewrite->rules;
}