我需要将此重写规则应用于多个自定义帖子类型。
目前它只使用一个cpt“articles”,但我需要它应用于“articles”&;“新闻稿”
如何将代码更改为这两种自定义帖子类型
/**
* Custom post type specific rewrite rules
* @return wp_rewrite Rewrite rules handled by WordPress
*/
function cpt_rewrite_rules($wp_rewrite)
{
// Here we\'re hardcoding the CPT in, article in this case
$rules = cpt_generate_date_archives(\'article\', $wp_rewrite);
$wp_rewrite->rules = $rules + $wp_rewrite->rules;
return $wp_rewrite;
}
add_action(\'generate_rewrite_rules\', \'cpt_rewrite_rules\');
/**
* Generate date archive rewrite rules for a given custom post type
* @param string $cpt slug of the custom post type
* @return rules returns a set of rewrite rules for WordPress to handle
*/
function cpt_generate_date_archives($cpt, $wp_rewrite)
{
$rules = array();
$post_type = get_post_type_object($cpt);
$slug_archive = $post_type->has_archive;
if ($slug_archive === false) {
return $rules;
}
if ($slug_archive === true) {
// Here\'s my edit to the original function, let\'s pick up
// custom slug from the post type object if user has
// specified one.
$slug_archive = $post_type->rewrite[\'slug\'];
}
$dates = array(
array(
\'rule\' => "([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})",
\'vars\' => array(\'year\', \'monthnum\', \'day\')
),
array(
\'rule\' => "([0-9]{4})/([0-9]{1,2})",
\'vars\' => array(\'year\', \'monthnum\')
),
array(
\'rule\' => "([0-9]{4})",
\'vars\' => array(\'year\')
)
);
foreach ($dates as $data) {
$query = \'index.php?post_type=\'.$cpt;
$rule = $slug_archive.\'/\'.$data[\'rule\'];
$i = 1;
foreach ($data[\'vars\'] as $var) {
$query.= \'&\'.$var.\'=\'.$wp_rewrite->preg_index($i);
$i++;
}
$rules[$rule."/?$"] = $query;
$rules[$rule."/feed/(feed|rdf|rss|rss2|atom)/?$"] = $query."&feed=".$wp_rewrite->preg_index($i);
$rules[$rule."/(feed|rdf|rss|rss2|atom)/?$"] = $query."&feed=".$wp_rewrite->preg_index($i);
$rules[$rule."/page/([0-9]{1,})/?$"] = $query."&paged=".$wp_rewrite->preg_index($i);
}
return $rules;
}
SO网友:Jevuska
@DarrenLee,要在代码中添加多个帖子类型,只需更改函数cpt_rewrite_rules
. 创建帖子类型数组,put函数cpt_generate_date_archives
内部回路。
function cpt_rewrite_rules($wp_rewrite)
{
$post_types = array( \'book\', \'movie\', \'etc\' ); //create post_types array
$rules = array();
foreach( $post_types as $post_type )
{
/* generate date archives */
$rule = cpt_generate_date_archives( $post_type, $wp_rewrite );
foreach( $rule as $k => $v )
$rules[ $k ] = $v;
}
$wp_rewrite->rules = $rules + $wp_rewrite->rules; // combine rules
return $wp_rewrite;
}
add_action(\'generate_rewrite_rules\', \'cpt_rewrite_rules\');