重写url自定义帖子类型时错误重定向到主页

时间:2016-07-27 02:54:06

标签: wordpress

我有自定义帖子类型

function anime_post_type() {
  register_post_type('anime', array(
    'labels' => array(
      'name' => 'Anime',
      'singular_name' => 'Anime',
      'add_new' => _x('Thêm Anime', 'animefrost'),
      'add_new_item' => 'Thêm Anime Mới',
      ),
    'show_ui' => true,
    'public' => true,
    'menu_icon' => 'dashicons-video-alt3',
    'exclude_from_search' => true,
    'hierarchical' => false,
    'rewrite' => array('slug' => 'story', 'with_front' => FALSE),
    'supports' => array('title', 'editor', 'thumbnail')
    )
  );
  //flush_rewrite_rules( false );
}

我想重写

http://localhost/Anime/story/one-pice/?ep=1http://localhost/Anime/story/one-pice/ep/1

使用我的代码

function add_query_vars_filter( $vars ){
  $vars[] = "ep";
  return $vars;
}
add_filter( 'query_vars', 'add_query_vars_filter' );


add_action('init', 'dcc_rewrite_rules');
function dcc_rewrite_rules() {
    add_rewrite_rule('^story/([^/]+)/ep/([0-9]+)$','index.php?post_type=anime&ep=$matches[2]','top');
    flush_rewrite_rules(false);
}

我的代码中有什么错误??? 请帮帮我,谢谢。

1 个答案:

答案 0 :(得分:0)

在rewrite_rules_array过滤器

上重写规则
add_filter('rewrite_rules_array', 'dcc_rewrite_rules');

现在添加新规则并将其退回

function dcc_rewrite_rules() {
    $aNewRules = array('story/([^/]+)/?$' => 'index.php?pagename=story&ep=$matches[1]');
    $aRules = $aNewRules + $aRules;
    return $aRules;
}

检查this

不要忘记保存永久链接

您可以通过$wp_query->query_vars['ep']

访问查询字符串
相关问题