我遇到了同样的问题,需要两个归档页面来定制帖子类型。我能够使用四个WP挂钩相当干净地完成这项工作
例如,我的自定义帖子类型是“member”。我需要为自定义meta\\u键“plus”设置为true的成员提供第二个存档url。
First we need to define the URL for the second archive url. Notice passing the "plus_member" var in the query string.
add_filter(\'rewrite_rules_array\', function($rules) {
return array_merge(array(
\'plus-members/?$\' => \'index.php?post_type=member&archive_type=archive&plus_member=1\',
\'plus-members/page/([0-9]{1,})/?$\' => \'index.php?post_type=member&archive_type=archive&plus_member=1&paged=$matches[1]\',
), $rules);
});
Next we need to allow the "plus_member" we are passing along
add_filter(\'query_vars\', function($vars) {
$vars[] = \'plus_member\';
return $vars;
});
Next we need to tell WP to use a different template file for this request instead of the default "archive-member.php"
add_filter(\'template_include\', function($template) {
global $wp_query;
if ($wp_query->get(\'post_type\') == \'member\' && $wp_query->get(\'plus_member\')) {
$template = locate_template(\'archive-plus-member.php\');
}
return $template;
});
Finally we need to alter the main query so we are not showing all members but only plus members. add_filter(\'pre_get_posts\', function($query) {
if ($query->is_main_query() && $query->is_archive() && $query->get(\'post_type\') == \'business\') {
if ($query->get(\'plus_member\')) {
$query->set(\'meta_key\', \'plus\');
$query->set(\'meta_value\', true);
}
}
return $query;
});
This should produce the result:
/成员/(加载)存档成员。php(显示)所有成员/plus成员/(加载)存档plus成员。php(显示)所有成员,其中meta\\u key plus==true