我创建了两种自定义帖子类型,分别为"test" 和"articles".
我正在尝试实现以下URL结构:
网站。com/测试/帖子标题-针对帖子类型test. 工作正常站点。com/测试/文章/帖子标题-针对帖子类型articles正如您所看到的,这一个前面有“test”(第一个cpt的名称)。尝试访问帖子会返回“找不到页面”。
我有updated permalinks 从…起Admin > Settings > Permalinks.
在自定义帖子类型时"articles" 注册我声明:
\'rewrite\' => array(\'slug\' => \'test/articles\')
有没有办法处理此URL格式?
更新时间:
$args = array(
    \'has_archive\' => false,
    \'hierarchical\' => true,
    \'public\' => true,
    \'label\'  => __( \'Test\', \'domain\'),
    \'supports\' => array( \'title\', \'editor\', \'author )
);
register_post_type( \'test\', $args );
$args = array(
    \'public\' => true,
    \'has_archive\' => true,
    \'label\' => __( \'Articles\', \'domain\' ),
    \'supports\'  => array( \'title\', \'editor\', \'author\' ),
    \'rewrite\' => array(\'slug\' => \'test/articles\')
);
register_post_type( \'articles\', $args );
 
                    最合适的回答,由SO网友:Adam 整理而成
                    问题是,无论何时,只要你尝试去一个帖子,比如:
http://example.com/test/articles/example-slug/
                    ↑      ↑
                    |      └ attempts to match "articles" as name/pagename query variable 
                    └ matches post_type "test"
 WordPress正在尝试匹配
/test/articles, 如中所示
articles 担任…的职务
post_type === test.
相反,您需要添加自定义重写规则(然后刷新重写规则):
function wpse221472_custom_rewrite_rules() {
  add_rewrite_rule(
    \'^test/(articles)/(.*)?\', 
    \'index.php?post_type=$matches[1]&name=$matches[2]\', 
    \'top\'
  );
}
add_action(\'init\', \'wpse221472_custom_rewrite_rules\');
 这将匹配:
                       $matches[1]  $matches[2]
                           ↓            ↓
http://example.com/test/articles/example-slug/
 不确定这是否是最好的方法,但它会起作用,只是要知道,您不能在
test post_type 那有点
articles 因为它不会被匹配;我们的重写规则将覆盖它。
如果你不介意的话,以上内容就足够了。