我找到了解决办法!Monkeyman Rewrite Analyzur插件非常有用:https://wordpress.org/plugins/monkeyman-rewrite-analyzer/
所以现在,我有了这样的工作URL。。。
single.php:
/magazine-name/issue-year/issue/article-name
archive.php:
/magazine-name/issue-year/issue
/magazine-name/issue-year
/magazine-name
在wp admin的自定义帖子编辑器页面上,我使用高级自定义字段插件来显示发布年份和发布。您还可以自己定义元字段。
接下来,我为发行年份和发行添加了重写标记:
function custom_rewrite_tag() {
add_rewrite_tag(\'%issue_year%\', \'([0-9]{4})\' );
add_rewrite_tag(\'%issue%\', \'([0-9]+)\' );
}
add_action(\'init\', \'custom_rewrite_tag\');
接下来,我为任意URL组合添加了重写规则:
function custom_rewrite_rule() {
// URL: /magazine/year/issue/title
add_rewrite_rule(\'^([^/]+)/([0-9]{4})/([0-9])/(.?.+?)?(:/([0-9]+))?/?$\', \'index.php?post_type=$matches[1]&issue_year=$matches[2]&issue=$matches[3]&name=$matches[4]\', \'top\');
// URL: /magazine/year/issue
add_rewrite_rule(\'^([^/]+)/([0-9]{4})/([0-9])?$\', \'index.php?post_type=$matches[1]&issue_year=$matches[2]&issue=$matches[3]\', \'top\');
// URL: /magazine/year
add_rewrite_rule(\'^([^/]+)/([0-9]{4})?$\', \'index.php?post_type=$matches[1]&issue_year=$matches[2]\', \'top\');
}
add_action(\'init\', \'custom_rewrite_rule\');
最后,我用发行年份和发行日期替换了自己的标准URL
function custom_permalink($url, $post) {
if ($post->post_type == \'magazine-name-1\' || $post->post_type == \'magazine-name-2\' || $post->post_type == \'magazine-name-2\' ) {
global $post;
$post_type = $post->post_type;
$issue_year = get_field(\'issue_year\', $post->ID); // ACF; for meta: get_post_meta($post->ID, \'$issue\', true);
$issue = get_field(\'issue\', $post->ID); // ACF
$url = str_replace( $post_type . \'/\', $post_type . \'/\' . $issue_year . \'/\' . $issue . \'/\' , $url);
}
return $url;
}
add_filter(\'post_type_link\', \'custom_permalink\', 10, 2);