好吧,在尝试自己做之后,就像我通常做的重写一样(但这次出于某种原因,没有运气)我已经找到了这个答案:
Custom post type yearly/ monthly archive
但仍然不适合我。 如果我记录规则,我可以找到正确的规则,它写得正确:
["it/press-release/([0-9]{4})/?$"]=>
string(50) "index.php?post_type=press_article&year=$matches[1]"
为了完整起见,FIRST行登录规则(参见下面的最后一行,它是对的):
array(309) {
["it/press-release/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/?$"]=>
string(87) "index.php?post_type=press_article&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]"
["it/press-release/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$"]=>
string(104) "index.php?post_type=press_article&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&feed=$matches[4]"
["it/press-release/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/(feed|rdf|rss|rss2|atom)/?$"]=>
string(104) "index.php?post_type=press_article&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&feed=$matches[4]"
["it/press-release/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/page/([0-9]{1,})/?$"]=>
string(105) "index.php?post_type=press_article&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&paged=$matches[4]"
["it/press-release/([0-9]{4})/([0-9]{1,2})/?$"]=>
string(71) "index.php?post_type=press_article&year=$matches[1]&monthnum=$matches[2]"
["it/press-release/([0-9]{4})/([0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$"]=>
string(88) "index.php?post_type=press_article&year=$matches[1]&monthnum=$matches[2]&feed=$matches[3]"
["it/press-release/([0-9]{4})/([0-9]{1,2})/(feed|rdf|rss|rss2|atom)/?$"]=>
string(88) "index.php?post_type=press_article&year=$matches[1]&monthnum=$matches[2]&feed=$matches[3]"
["it/press-release/([0-9]{4})/([0-9]{1,2})/page/([0-9]{1,})/?$"]=>
string(89) "index.php?post_type=press_article&year=$matches[1]&monthnum=$matches[2]&paged=$matches[3]"
["it/press-release/([0-9]{4})/?$"]=>
string(50) "index.php?post_type=press_article&year=$matches[1]"
但它仍然没有用。如果我尝试导航到it/press-release/2016
(是的,那些日期有帖子)我会被重定向到首页。
我必须说几件事:
我的自定义帖子类型注册如下:
$args = array(
'public' => true,
'supports' => array('title','editor','page-attributes'),
'description' => 'Rassegna stampa',
'labels' => $labels, // omitting here since not relevant
'rewrite' => array('slug' => pll__('rassegna-stampa')),
'has_archive' => true,
'menu_icon' => 'dashicons-media-document'
);//end args
register_post_type('press_article',$args);
有人能发现问题吗?
答案 0 :(得分:0)
我在最近的WorPress项目中做了类似的事情,其中客户希望基于年份的新闻导航(类别新闻中的帖子)。我使用自定义重写规则来做到这一点。
注册自定义重写规则
function px_generate_rewrite_rules($wp_rewrite)
{
$new_rules = array(
'([^/]+)/archive/(\d+)/?$' => 'index.php?category_name=$matches[1]&px_archive_year=$matches[2]'
);
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_action('generate_rewrite_rules', 'px_generate_rewrite_rules');
注册自定义查询var
function px_query_vars($qvars)
{
$qvars[] = 'px_archive_year';
return $qvars;
}
add_filter('query_vars', 'px_query_vars');
添加自定义查询
function px_pre_posts($query)
{
if( ! is_admin() )
{
if( $query->is_main_query() && is_category() )
{
set_query_var('date_query', array(
array(
array(
'year' => get_query_var('px_archive_year', date('Y')),
)
)
));
return;
}
}
}
add_action('pre_get_posts', 'px_pre_posts');
请记住,如果更改某些内容,则必须刷新重写规则。完成后删除此代码。
add_action('init', function() {
flush_rewrite_rules();
});
注意!请注意页面标题。每个页面都应该有一个唯一的标题。