这里有很多假设,但也许你可以澄清一下。这假设您制作的CPT可能如下所示:
domain.com/page/class/english-pro
并将其转换为
domain.com/page/class-english-pro
不管怎样。URL正在获取参数并修改查询以显示页面。这里添加了CPT,所以这个示例可以适用于任何开箱即用的人。添加permalink重写是为了调整permalink。
我不会说这就是答案,但这里有些东西可能会对你有所帮助。
<?php
if ( ! class_exists( \'PrefixClassesRewrites\' ) ) {
class PrefixClassesRewrites {
const POST_TYPE_SLUG = \'classes\';
function __invoke() {
add_action( \'init\', array ( $this, \'prefix__init\' ) );
add_action( \'init\', array ( $this, \'prefix__init_cpt\' ), 0 );
add_action( \'pre_get_posts\', array ( $this, \'prefix__pre_get_posts\' ) );
add_filter( \'post_type_link\', array ( $this, \'prefix__pre_post_link\' ), 3 );
add_filter( \'post_link\', array ( $this, \'prefix__pre_post_link\' ), 3 );
}
/**
* Register the custom post type.
*/
public function prefix__init_cpt() {
$rewrite = array (
\'slug\' => \'page/class\', // get\'s closer but will end with / not -
\'with_front\' => false,
\'pages\' => true,
\'feeds\' => true,
);
register_post_type( static::POST_TYPE_SLUG, array (
\'label\' => __( \'Classes Post Type\', \'text_domain\' ),
\'description\' => __( \'Classes Post Type Description\', \'text_domain\' ),
\'rewrite\' => $rewrite,
\'hierarchical\' => false,
\'public\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'menu_position\' => 5,
\'show_in_admin_bar\' => true,
\'show_in_nav_menus\' => true,
\'can_export\' => true,
\'has_archive\' => true,
\'exclude_from_search\' => false,
\'publicly_queryable\' => true,
\'capability_type\' => \'page\',
) );
}
/**
* Add the rewrite rule and allow custom query params
*/
public function prefix__init() {
// custom query params that we check for later
add_rewrite_tag( \'%_page_class%\', \'_page_class\' );
add_rewrite_tag( \'%_language%\', \'([a-zA-Z\\d\\-_+]+)\' );
add_rewrite_tag( \'%_need%\', \'([a-zA-Z\\d\\-_+]+)\' );
// rewrite rule to transform the URL into params
add_rewrite_rule( \'page/class-([a-zA-Z\\d\\-_+]+)-([a-zA-Z\\d\\-_+]+)?\', \'index.php?_page_class=1&_language=$matches[1]&_need=$matches[2]\', \'top\' );
flush_rewrite_rules(); // <-- for testing only | removed once the rewrite rules are in place
}
/**
* Modify the query to convert our params to a custom post type pagename
*
* @param $query
*/
public function prefix__pre_get_posts( $query ) {
// Check for our custom query param. Really just a flag to do this
if ( isset( $query->query_vars[ \'_page_class\' ] ) ) {
// Convert the variables to a custom post type name.
$name = implode( \'-\', array (
$query->query_vars[ \'_language\' ],
$query->query_vars[ \'_need\' ],
) );
// Add the name and post_type to the query.
$query->query_vars[ \'name\' ] = $name;
$query->query_vars[ \'post_type\' ] = array ( static::POST_TYPE_SLUG );
// Nothing else to do here.
return;
}
}
/**
* Rewrite the permalink to match our setup.
*
* @param string $permalink
* @param null|WP_Post $post
* @param bool $leavename
*
* @return string
*/
public function prefix__pre_post_link( $permalink = \'\', $post = null, $leavename = false ) {
return str_replace( \'/page/class/\', \'/page/class-\', $permalink );
}
}
$prefixClassesRewrites = new PrefixClassesRewrites();
$prefixClassesRewrites(); // kick off the process
}