重写规则是错误的。例如,您想从community/tag/easter
到\'index.php?pagename=community&stencil-tag=easter\'
, 所以正则表达式应该包含community/tag/
不仅如此tag/
. 而且\\d
仅匹配数字,但标记值是字符串。同样适用于“重写模具”标记。你可以使用.
匹配任何字符,包括数字和字符串。
add_action(\'init\',\'add_community_rewrite_rules\');
function add_community_rewrite_rules() {
add_rewrite_rule(
\'^community/tag/(.*)$\',
\'index.php?pagename=community&stencil-tag=$matches[1]\',
\'top\'
);
add_rewrite_rule(
\'^community/type/(.*)$\',
\'index.php?pagename=community&stencil-type=$matches[1]\',
\'top\'
);
}
虽然这些重写规则可能是正确的,但我认为您应该使用另一种方法。使用WordPress为
projects
岗位类型。如果需要特定的模板,请创建文件
archive-projects.php
在主题文件夹中。你可以从
projects
到
community
注册帖子类型时,如果希望该URL用于帖子类型存档:
add_action( \'init\', function() {
$args = array(
//Your args here
\'rewrite\' => array( \'slug\' => \'community\' ),
);
register_post_type( \'projects\', $args );
} );
然后,在注册分类法时,还可以构建重写规则:
add_action( \'init\', function() {
//Custom post type
$args = array(
//Your args here
\'rewrite\' => array( \'slug\' => \'community\' ),
);
register_post_type( \'projects\', $args );
//Custom taxonomies: stencil-tag
$args = array(
//Your args here
\'rewrite\' => array( \'slug\' => \'community/tag\' ),
);
register_taxonomy( \'stencil-tag\', \'projects\', $args );
//Custom taxonomies: stencil-tag
$args = array(
//Your args here
\'rewrite\' => array( \'slug\' => \'community/type\' ),
);
register_taxonomy( \'stencil-type\', \'projects\', $args );
} );
现在,重写规则由WordPres自动处理,您无需呈现页面并在主查询中获取页面内的帖子时对其进行二次查询。