日期档案固定链接修改

时间:2017-07-03 作者:norixxx

我对日期存档permalink做了一些调整。

function my_rewrite_rules($wp_rewrite){
    $rules = array();
    $rules[\'news/([0-9]{4})/?$\'] = \'index.php?year=$matches[1]\';
    $wp_rewrite->rules = $rules + $wp_rewrite->rules;
}

add_action(\'generate_rewrite_rules\', \'my_rewrite_rules\');
然后点击下面的url。

http://blahblah.blahblah/wp/news/2017/
这成功显示了属于2017年的帖子。

现在我想为日期存档生成链接,但这并不能生成我想要的代码。

get_year_link($year);
仍然生成默认的永久链接,如下所示:

http://blahblah.blahblah/wp/date/2017/
那么,我如何告诉wordpress使用什么permastructure呢?否则我可能不得不硬编码。。。

2 个回复
SO网友:Milo

您可以通过直接修改date_structure:

function wpd_change_date_structure(){
    global $wp_rewrite;
    $wp_rewrite->date_structure = \'news/%year%/%monthnum%/%day%\';
}
add_action( \'init\', \'wpd_change_date_structure\' );
更改后不要忘记刷新重写规则。

WordPress将使用它来生成传入请求的重写规则,以及使用API函数时的URL输出,如get_year_link, 所以不需要过滤器。

SO网友:Luca Reghellin

是的,norixxx,你上面的评论是正确的答案:

add_filter(\'year_link\', \'change_year_link\', 10, 2);
function change_year_link($yearlink, $year){
  return home_url(user_trailingslashit(\'news/\'.$year));
}
谢谢分享!

在下面,用户可以找到“扩展”版本:

// generate /year, /year/month, /year/month/post-slug rules
add_action(\'generate_rewrite_rules\', \'rewrite_rules\');
function rewrite_rules($wp_rewrite){
  $rules = array();
  $rules[\'news/([0-9]{4})/?$\'] = \'index.php?year=$matches[1]\';
  $rules[\'news/([0-9]{4})/([0-9]{2})/?$\'] = \'index.php?year=$matches[1]&monthnum=$matches[2]\';
  $rules[\'news/([0-9]{4})/([0-9]{2})/([^/]+)/?$\'] = \'index.php?year=$matches[1]&monthnum=$matches[2]&name=$matches[3]\';
  $wp_rewrite->rules = $rules + $wp_rewrite->rules;
}


add_filter(\'year_link\', \'change_year_link\', 10, 2);
function change_year_link($yearlink, $year){
  return home_url(user_trailingslashit(\'news/\'.$year));
}

add_filter(\'month_link\', \'change_month_link\', 10, 2);
function change_month_link($yearlink, $year, $month){
  return home_url(user_trailingslashit(\'news/\'.$year.\'/\'.$month));
}

// this last is for generating /year/month/post-slug links
// I think it can be optional. Keep in mind that by applying this filter,
// You won\'t be able to manually modify the slug in admin edit post view
add_filter(\'post_link\', \'set_post_links\' , 10, 2);
function set_post_links($permalink, $post){
  if(is_wp_error($post) || empty($post->post_name)) return $permalink;

  if(\'post\' == $post->post_type){
    $year = get_the_date(\'Y\', $post->ID);
    $month = get_the_date(\'m\', $post->ID);
    return home_url(user_trailingslashit("$year/$month/$post->post_name"));
  }

  return $permalink;
} 


// If you use the post_link filter, 
// probably you\'ll want to auto-update post slugs upon post title modifications. 
// Here\'s how:
add_filter(\'wp_insert_post_data\', \'update_post_data\' , 10, 2);
function update_post_data($data, $postarr){
  // protect non-post posts
  if($data[\'post_type\'] !== \'post\') return $data;

  $data[\'post_name\'] = wp_unique_post_slug(sanitize_title($data[\'post_title\']), $postarr[\'ID\'], $data[\'post_status\'], $data[\'post_type\'], $data[\'post_parent\']);
  return $data;
}

结束

相关推荐

Change Taxonomy Permalinks

我有自定义帖子,我创建了一个显示所有自定义帖子的页面。示例:www.example.com/archive-page我想知道是否可以更改与此自定义帖子相关的类别和标签的永久链接。现在我有:www.example.com/my-custom-post-type-cats/my-category-1www.example.com/my-custom-post-type-tags/my-tag-1</我想要这样的东西:www.example.com/archive-page?category=1www.e