试试这个。它将替换段塞project
父级和子级均无404错误。
/**
* Strip the slug out of a hierarchical custom post type
*/
if ( !class_exists( \'project_Rewrites\' ) ) :
class project_Rewrites {
private static $instance;
public $rules;
private function __construct() {
/* Don\'t do anything, needs to be initialized via instance() method */
}
public static function instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new project_Rewrites;
self::$instance->setup();
}
return self::$instance;
}
public function setup() {
add_action( \'init\', array( $this, \'add_rewrites\' ), 20 );
add_filter( \'request\', array( $this, \'check_rewrite_conflicts\' ) );
add_filter( \'project_rewrite_rules\', array( $this, \'strip_project_rules\' ) );
add_filter( \'rewrite_rules_array\', array( $this, \'inject_project_rules\' ) );
}
public function add_rewrites() {
add_rewrite_tag( "%project%", \'(.+?)\', "project=" );
add_permastruct( \'project\', "%project%", array(
\'ep_mask\' => EP_PERMALINK
) );
}
public function check_rewrite_conflicts( $qv ) {
if ( isset( $qv[\'project\'] ) ) {
if ( get_page_by_path( $qv[\'project\'] ) ) {
$qv = array( \'pagename\' => $qv[\'project\'] );
}
}
return $qv;
}
public function strip_project_rules( $rules ) {
$this->rules = $rules;
# We no longer need the attachment rules, so strip them out
foreach ( $this->rules as $regex => $value ) {
if ( strpos( $value, \'attachment\' ) )
unset( $this->rules[ $regex ] );
}
return array();
}
public function inject_project_rules( $rules ) {
# This is the first \'page\' rule
$offset = array_search( \'(.?.+?)/trackback/?$\', array_keys( $rules ) );
$page_rules = array_slice( $rules, $offset, null, true );
$other_rules = array_slice( $rules, 0, $offset, true );
return array_merge( $other_rules, $this->rules, $page_rules );
}
}
project_Rewrites::instance();
endif;